微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Unity VR中使用Waypoint实现脚本时出错

如何解决在Unity VR中使用Waypoint实现脚本时出错

我是正在学习Unity的菜鸟。我正在创建一个脚本,将摄像机移动到目的地,并在到达时停止。按下“播放”按钮三秒钟后,播放器(相机)离开,并沿着路线进行了很好的移动,但问题出在到达之后。但是,到达目的地时,相机不会停下来并在现场旋转。 (Debug.Log ("move done");不会出现在此控制台窗口中。)

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

public class MMoveWayPoints1 : MonoBehavIoUr
{
    // move complete flag
    private bool move_done = false;
    // moving speed
    public float speed = 0.5f;
    // factor for rotation speed
    public float damping = 3.0f;

    private Transform tr;
    private Transform[] Points;
    private int next_idx = 1;

    private Transform camTr;
    private CharacterController cc;

    // Use this for initialization
    void Start()
    {
        // get the current transform of the player
        tr = GetComponent<Transform>();
        // get the transform component of the main camera 
        camTr = Camera.main.GetComponent<Transform>();
        // get the character controller component of the Player
        cc = GetComponent<CharacterController>();

        // extract all the waypoints
        Points = GameObject.Find("Waypoints").GetComponentsInChildren<Transform>();

       
    }

    // Update is called once per frame
    void Update()
    {

         if (!move_done)
        {
            StartCoroutine(MovetoWaypoint());
        }
        // stop move if the move is doen
        
    }

    // move to waypoint
    IEnumerator MovetoWaypoint()
    {
        yield return new WaitForSeconds(3f);

        // compute the vector to the next wypoint
        Vector3 direction = Points[next_idx].position - tr.position;
        // compute the rotation as quaternion
        Quaternion rot = Quaternion.LookRotation(direction);
        // rotate smoothly
        tr.rotation = Quaternion.Slerp(tr.rotation,rot,Time.deltaTime * damping);
        // translate
        tr.Translate(Vector3.forward * Time.deltaTime * speed);

    }

    // event for waypoint collision
    private void OnTriggerEnter(Collider other)
    {
        // check if the Tag is the WAY_POINT
        if (other.CompareTag("WAY_POINT"))
        {
            // increase the waypoint index
            if (next_idx < Points.Length - 1)
            {
                next_idx++;
                Debug.Log("waypoint: " + next_idx.ToString());
            }
            else 
            {
                move_done = true;
                Debug.Log("move done");
            }
        }
    }

    // return the move doen flag
    public bool IsMoveDone()
    {
        return move_done;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。