Unity3d 在统一中检测对特定对象的触摸

Unity3d 在统一中检测对特定对象的触摸,unity3d,Unity3d,在Unity 2D中开发android游戏。长话短说,我在场景中有两个单独的对象,上面附着了此脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class SwipeScript : MonoBehaviour { Vector2 startPos, endPos, direction; float touchTimeStart, touchTime

在Unity 2D中开发android游戏。长话短说,我在场景中有两个单独的对象,上面附着了此脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwipeScript : MonoBehaviour
{
    Vector2 startPos, endPos, direction;
    float touchTimeStart, touchTimeFinish, timeInterval;
    public static int brojbacanja=0;
    public static bool bacenaprva = false;

    [Range (0.05f, 1f)]
    public float throwForce = 0.3f;

    void Update(){
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began && brojbacanja == 0) {

                    touchTimeStart = Time.time;
                    startPos = Input.GetTouch (0).position;

        }
            if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended && brojbacanja == 0) {
                touchTimeFinish = Time.time;
                timeInterval = touchTimeFinish - touchTimeStart;
                endPos = Input.GetTouch (0).position;
                direction = startPos - endPos;
                GetComponent<Rigidbody2D> ().AddForce (-direction / timeInterval * throwForce);
                brojbacanja = 1;
                bacenaprva = true;
            }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类SwipScript:MonoBehavior
{
矢量2起始位置、结束位置、方向;
浮动touchTimeStart、touchTimeFinish、时间间隔;
公共静态int brojbacanja=0;
公共静态bool bacenaprva=假;
[范围(0.05f,1f)]
公众浮力=0.3f;
无效更新(){
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Start&&brojbacanja==0){
touchTimeStart=Time.Time;
startPos=Input.GetTouch(0).position;
}
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Ended&&brojbacanja==0){
touchTimeFinish=Time.Time;
时间间隔=touchTimeFinish-touchTimeStart;
endPos=Input.GetTouch(0).position;
方向=开始位置-结束位置;
GetComponent().AddForce(-direction/timeInterval*throwForce);
brojbacanja=1;
bacenaprva=真;
}
}
}
这让我几乎可以在屏幕上的任何地方滑动并抛出一个它所连接的对象。因为我有两个不同的物体,我想分开扔,我想改变一下,当我触摸我想扔的物体时,另一个保持静止。我以前读过关于这个问题的文章,并尝试使用Raycast和OnMouseDown(),但不知道如何实现它


如果有人能提供帮助,我们将不胜感激。

由于此脚本将出现在场景中的每个对象上,因此您需要有一个额外的变量,并检查初始触摸是否与您的对象相交

如果您使用的是3D对象(我假设是因为您提到了光线跟踪),那么您需要将屏幕上的触摸位置转换为光线,然后进行投射以查看它是否与您的对象相交。您的对象必须有碰撞才能工作

public class SwipeScript : MonoBehaviour
{
    // added these two values, set the coll value to your collider on this object.
    bool isTouching;
    public Collider coll;

    Vector2 startPos, endPos, direction;
    float touchTimeStart, touchTimeFinish, timeInterval;
    public static int brojbacanja=0;
    public static bool bacenaprva = false;

    [Range (0.05f, 1f)]
    public float throwForce = 0.3f;

    void Update(){
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began && brojbacanja == 0) {
                if(IsTouchOverThisObject(Input.GetTouch(0))) {
                    isTouching = true;
                    touchTimeStart = Time.time;
                    startPos = Input.GetTouch (0).position;
                }

        }
            if (isTouching && Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended && brojbacanja == 0) {
                isTouching = false;
                touchTimeFinish = Time.time;
                timeInterval = touchTimeFinish - touchTimeStart;
                endPos = Input.GetTouch (0).position;
                direction = startPos - endPos;
                GetComponent<Rigidbody2D> ().AddForce (-direction / timeInterval * throwForce);
                brojbacanja = 1;
                bacenaprva = true;
            }
    }

    bool IsTouchOverThisObject(Touch touch) {
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
         RaycastHit hit;

         // you may need to adjust the max distance paramter here based on your
         // scene size/scale.
         return coll.Raycast(ray, out hit, 1000.0f); 
    }
}
公共类SwipScript:MonoBehavior
{
//添加这两个值后,将coll值设置为此对象上的碰撞器。
布尔·伊斯图辛;
公共对撞机;
矢量2起始位置、结束位置、方向;
浮动touchTimeStart、touchTimeFinish、时间间隔;
公共静态int brojbacanja=0;
公共静态bool bacenaprva=假;
[范围(0.05f,1f)]
公众浮力=0.3f;
无效更新(){
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Start&&brojbacanja==0){
if(IsTouchOverthis对象(Input.GetTouch(0))){
isTouching=true;
touchTimeStart=Time.Time;
startPos=Input.GetTouch(0).position;
}
}
如果(iTouching&&Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Ended&&brojbacanja==0){
isTouching=false;
touchTimeFinish=Time.Time;
时间间隔=touchTimeFinish-touchTimeStart;
endPos=Input.GetTouch(0).position;
方向=开始位置-结束位置;
GetComponent().AddForce(-direction/timeInterval*throwForce);
brojbacanja=1;
bacenaprva=真;
}
}
bool i触摸该对象(触摸){
Ray-Ray=Camera.main.screenpointoray(新矢量3(touch.position.x,touch.position.y,0));
雷卡斯特击中;
//您可能需要根据您的需要在此处调整“最大距离”参数
//场景大小/比例。
返回coll.Raycast(光线,外击,1000.0f);
}
}