Unity touch不起作用

时间:2014-04-13 01:07:45

标签: unity3d unityscript

嘿,我有团结接受触摸注册的问题。我正在创建的程序需要能够在阻止它的项目海洋中注册特定项目的触摸。目前,Unity没有注册我的触摸,因为我试图打印消息以查看它的结束位置并且没有注册触摸。任何帮助修复我的代码将不胜感激。 PS:代码是用Javascript编写的。

    #pragma strict
var target: GameObject;
var SpawnPoint: Transform;
var TargetCollider : BoxCollider;
var hit : RaycastHit;
var clone: GameObject;

function Start () {

    var x = Random.Range(-14,17);
    var y = Random.Range(-6,9);
    var z = Random.Range(2,3);
    SpawnPoint.position = Vector3(x,y,0);
    yield WaitForSeconds(5);
    clone = Instantiate(target, SpawnPoint.position, SpawnPoint.rotation);  
}

function Update () 
{
            /*
            if (Input.GetTouch(0))  
            {

            if (hit.collider != null)
            {
            var hit = RaycastHit;
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            TargetCollider = target.GetComponent(BoxCollider);

            if (collider.Raycast(ray, RaycastHit, 100.00f)) {
                if (hit.collider != null)
                {
                    Destroy(clone);
                    Application.LoadLevel("Splash Screen Win");
                }   

            }
        }   

}
*/
                var tapCount = Input.touchCount;
                for (var i = 0; i < tapCount; i++)
                {
                var touch = Input.GetTouch(i);
                var hit : RaycastHit;
                if (Physics.Raycast (Camera.main.ScreenPointToRay(touch.position)))
                {
                    print("test"); 
                    /*if (hit.collider != null)
                    {
                    Destroy(clone);
                    Application.LoadLevel("Splash Screen Win");
                    }*/
                }


                }



}

1 个答案:

答案 0 :(得分:0)

试试这个

`function Update() {

            var tapCount = Input.touchCount;
            for (var i = 0; i < tapCount; i++)
            {

            var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
            var hit : RaycastHit;
            if (Physics.Raycast (ray, 100))
            {
                print("test"); 

            }


            }

}`

您可能需要将目标目标对象放在单独的图层上,这样您就可以在该图层上专门进行光线投射,并忽略不同图层上的其他对象。

Check out how to create layer masks