0

我有一辆车,有四个 3D 文本 Accelerate、Brake、Left 和 Right。都有盒子对撞机。我已将以下脚本附加到主摄像机

using UnityEngine;
using System.Collections;

public class GamePlayCamera : MonoBehaviour {

void FixedUpdate ()
    {
        if ( Application.platform == RuntimePlatform.IPhonePlayer ||
            Application.platform == RuntimePlatform.Android )
        {
            if ( Input.touchCount <= 0 )
                return;

            for(int i = 0 ; i<Input.touches.Length; i++)
            {

                Touch touch  = Input.touches[i];
                if ( touch.phase == TouchPhase.Began )
                {

                    OnTouchBegan( touch.position );
                }
                else if(touch.phase == TouchPhase.Ended)
                {
                    OnTouchEnd( touch.position );
                }
            }
        }
        else
        {
            if ( Input.GetMouseButtonDown( 0 ) )
            {
                Debug.Log(Input.mousePosition);
                OnTouchBegan( Input.mousePosition );
            }
            else if(Input.GetMouseButtonUp(0))
            {
                OnTouchEnd( Input.mousePosition );
            }
        }
    }

        void OnTouchBegan (Vector2 screenPos)
        {
            Ray ray = Camera.main.ScreenPointToRay( screenPos );
            RaycastHit hit;

            if ( Physics.Raycast( ray, out hit ) )
            {
                if(hit.collider.gameObject.name.Equals("Acc"))
                {
                    hit.collider.gameObject.SendMessage("Accel", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("brk"))
                {
                    hit.collider.gameObject.SendMessage("TouchDown", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("LFT"))
                {
                    hit.collider.gameObject.SendMessage("TouchDown", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("RHT"))
                {
                    hit.collider.gameObject.SendMessage("TouchDown", SendMessageOptions.DontRequireReceiver);
                }

            }

        }

        void OnTouchEnd (Vector2 screenPos)
        {
            Ray ray = Camera.main.ScreenPointToRay( screenPos );
            RaycastHit hit;

            if ( Physics.Raycast( ray, out hit ) )
            {
                if(hit.collider.gameObject.name.Equals("Acc"))
                {
                    hit.collider.gameObject.SendMessage("DAccel", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("brk"))
                {
                    hit.collider.gameObject.SendMessage("TouchUp", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("LFT"))
                {
                    hit.collider.gameObject.SendMessage("TouchUp", SendMessageOptions.DontRequireReceiver);
                }
                else if(hit.collider.gameObject.name.Equals("RHT"))
                {
                    hit.collider.gameObject.SendMessage("TouchUp", SendMessageOptions.DontRequireReceiver);
                }

            }

        }

}

汽车运动的逻辑很好,我用鼠标测试过多次。问题是多点触控无法正常工作。MultiTouch 第一次工作,但在单击按钮几次后,它开始表现得很奇怪。当我单击左键时,Accelerate 的功能开始工作,有时什么也没有发生。谁能告诉我问题是什么?我是 Unity 新手,遇到了一些问题。请帮忙。谢谢。

4

0 回答 0