Unity 5固定更新

时间:2016-06-03 15:45:30

标签: c# unity3d unity5 raycasting

在我用Unity 5编写的项目中,我正在使用raycast执行对象触摸操作。但是,由于我的物体正在移动,有时当我触摸时,光线投射无法到达我的身体。作为解决方案,使用固定更新,我在项目设置上将固定时间步长从0.02设置为0.01。然而,在这个时候,它表现得好像我触摸了两次,它接受了两次触摸。我不应该使用更新功能,我需要使用固定更新。我该如何解决这个问题呢?

以下是我的代码:

   void FixedUpdate()
{
    if (PlayerPrefs.GetInt ("oyunsonu") == 0) {

        // Code for OnMouseDown in the iPhone. Unquote to test.
        RaycastHit hit = new RaycastHit ();
        for (int i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch (i).phase.Equals (TouchPhase.Began)) {
                Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);

                touchY = Input.GetTouch (i).position.y;
                if (Physics.Raycast (ray, out hit)) {

                    //Debug.Log (hit.transform.gameObject.name.ToString ());

                    GameObject myBallObject;
                    myBallObject = GameObject.FindWithTag ("Ball");

                    Instantiate (effect, myBallObject.transform.position, transform.rotation);
                    GetComponent<AudioSource> ().PlayOneShot (saundFile [3], 1);
                    //TextAnimation.Show ();
                    TouchObjectScript myTouchObjectScript = myBallObject.GetComponent<TouchObjectScript> ();
                    myTouchObjectScript.BallForce (hit.transform.gameObject.tag.ToString (), touchY, screenHeight);

                    //PlayerPrefs.SetInt ("aaa", 0);

                }

            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

使用Update()检测输入或移动非物理/刚体对象。您使用FixedUpdate()移动物理对象。观看Unity的UPDATE AND FIXEDUPDATE视频。

每次有触摸和光线投射时都要GameObject.FindWithTag("Ball");做好。不断检查PlayerPrefs.GetInt("oyunsonu")也不好。如果它们将在循环中多次使用,请缓存它们。以下是您的固定代码。

AudioSource myAudio;
GameObject myBallObject;
TouchObjectScript myTouchObjectScript;
int oyunsonu = 0;

void Start()
{
    myAudio = GetComponent<AudioSource>();
    myBallObject = GameObject.FindWithTag("Ball");
    myTouchObjectScript = myBallObject.GetComponent<TouchObjectScript>();
    oyunsonu = PlayerPrefs.GetInt("oyunsonu");
}

void Update()
{
    if (oyunsonu == 0)
    {

        // Code for OnMouseDown in the iPhone. Unquote to test.
        RaycastHit hit = new RaycastHit();
        for (int i = 0; i < Input.touchCount; ++i)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);

                touchY = Input.GetTouch(i).position.y;
                if (Physics.Raycast(ray, out hit))
                {
                    //Debug.Log (hit.transform.gameObject.name.ToString ());
                    Instantiate(effect, myBallObject.transform.position, transform.rotation);
                    myAudio.PlayOneShot(saundFile[3], 1);
                    //TextAnimation.Show ();
                    myTouchObjectScript.BallForce(hit.transform.gameObject.tag.ToString(), touchY, screenHeight);
                }

            }
        }
    }

}