为什么相机不旋转以面对第一个航路点?

时间:2019-02-27 17:19:35

标签: c# unity3d

游戏开始时,从数组中选择一个随机航路点。然后,摄像头应旋转以面对所选的随机航点,并开始向其移动。

摄像机到达航点后,应等待3秒钟,然后旋转到正面并朝下一个随机航点移动。

我遇到的问题是CREATE TABLE ETMP_TESTE AS SELECT * FROM SAJ.EASJOBJETO WHERE 1 = 0; 。照相机在开始向第一个航点移动之前不会旋转。相反,它向后移向第一个航路点。然后,当它到达航路点时,它会等待3秒钟旋转并移向下一个航路点。

除了相机不会旋转以面对第一个选定的随机航点之外,它都工作正常。它正在向后移动,而没有先旋转面对它。

这是我的代码:

航点脚本

Start()

看镜头脚本

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

public class Waypoints : MonoBehaviour
{
    public GameObject[] waypoints;
    public GameObject player;
    public float speed = 5;
    public float WPradius = 1;
    public LookAtCamera lookAtCam;

    private int current = 0;
    private bool rot = false;

    public void Init()
    {
        waypoints = GameObject.FindGameObjectsWithTag("Target");

        if(waypoints.Length > 0)
        {
            StartCoroutine(RotateFacingTarget(waypoints[UnityEngine.Random.Range(0, waypoints.Length)].transform));
        }
    }

    void Update()
    {
        if (waypoints.Length > 0)
        {
            if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
            {
                current = UnityEngine.Random.Range(0, waypoints.Length);
                rot = false;
                StartCoroutine(RotateFacingTarget(waypoints[current].transform));

                if (current >= waypoints.Length)
                {
                    current = 0;
                }
            }

            if (rot)
                transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
        }
    }

    IEnumerator RotateFacingTarget(Transform target)
    {
        yield return new WaitForSeconds(3);

        lookAtCam.target = target;
        rot = true;
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:2)

我们假设正在调用Waypoints.Init(),并且您的waypoints变量的数组为3。

  • Waypoints.Init()启动协程
    • 您的协程等待3秒
    • 3秒钟后,您设置Slerp面向镜头位置的相机目标
  • Update在其第一帧显示waypoints.Length > 0 == true
    • 它离目标很近,并且rot为假,因此它不会移动

现在,您正在等待3秒钟,没有旋转,也没有移动。

  • 协程的3秒等待时间到了,并开始向目标旋转
  • rot现在在轮换开始时为true,因此您的Update方法也开始向目标移动

在操作顺序的工作方式上似乎没有逻辑。如果需要按照您的描述进行操作,我建议您对目标进行不同的操作。

我已经使用枚举实现了以下内容:

航点

public class Waypoints : MonoBehaviour
{
    private GameObject[] waypoints;
    private Transform currentWaypoint;

    private enum CameraState
    {
        StartRotating,
        Rotating,
        Moving,
        Waiting
    }

    private CameraState cameraState;

    public GameObject player;
    public float speed = 5;
    public float WPradius = 1;
    public LookAtCamera lookAtCam;

    private int current = 0;
    private bool isCameraRotating = false;

    void Start()
    {
        cameraState = CameraState.StartRotating;
    }

    void Update()
    {
        switch (cameraState)
        {
            // This state is used as a trigger to set the camera target and start rotation
            case CameraState.StartRotating:
            {
                // Sanity check in case the waypoint array was set to length == 0 between states
                if (waypoints.Length == 0)
                    break;

                // Tell the camera to start rotating
                currentWaypoint = waypoints[UnityEngine.Random.Range(0, waypoints.Length)].transform;
                lookAtCam.target = currentWaypoint;
                cameraState = CameraState.Rotating;

                break;
            }

            // This state only needs to detect when the camera has completed rotation to start movement
            case CameraState.Rotating:
            {
                if (lookAtCam.IsFinishedRotating)
                    cameraState = CameraState.StartMoving;

                break;
            }

            case CameraState.Moving:
            {
                // Move
                transform.position = Vector3.MoveTowards(transform.position, currentWaypoint.position, Time.deltaTime * speed);

                // Check for the Waiting state
                if (Vector3.Distance(currentWaypoint.position, transform.position) < WPradius)
                {
                    // Set to waiting state
                    cameraState = CameraState.Waiting;

                    // Call the coroutine to wait once and not in CameraState.Waiting
                    // Coroutine will set the next state
                    StartCoroutine(WaitForTimer(3));
                }

                break;
            }
            case CameraState.Waiting:
                // Do nothing. Timer has already started
                break;
        }
    }

    IEnumerator WaitForTimer(float timer)
    {
        yield return new WaitForSeconds(timer);
        cameraState = CameraState.StartRotating;
    }

    public void RefreshWaypoints()
    {
        waypoints = GameObject.FindGameObjectsWithTag("Target");
    }
}

LookAtCamera

public class LookAtCamera : MonoBehaviour
{
    // Values that will be set in the Inspector
    public Transform target;
    public float RotationSpeed;

    private float timer = 0.0f;
    public bool IsRotationFinished
    {
        get { return timer > 0.99f; }
    }

    // Update is called once per frame
    void Update()
    {
        if (target != null && timer < 0.99f)
        {
            // Rotate us over time according to speed until we are in the required rotation
            transform.rotation = Quaternion.Slerp(transform.rotation,
                Quaternion.LookRotation((target.position - transform.position).normalized),
                timer);

            timer += Time.deltaTime * RotationSpeed;
        }
    }
}