gpt4 book ai didi

c# - 为什么相机不旋转以面向第一个航路点?

转载 作者:行者123 更新时间:2023-11-30 22:55:54 25 4
gpt4 key购买 nike

游戏开始时,会从数组中随机选择一个航路点。然后相机应旋转以面向选定的随机航路点并开始朝它移动。

一旦相机到达航路点,它应该等待 3 秒,然后旋转面向下一个随机航路点并移动。

我遇到的问题是在 Start() 中。相机在开始朝向第一个航路点移动之前不会旋转以面向第一个航路点。相反,它向后移动到第一个航路点。然后,当它到达航路点时,等待 3 秒旋转并移动到下一个航路点。

它工作正常,只是相机不会旋转以面向第一个选定的随机航路点。它在没有先旋转面对它的情况下向后移动。

这是我的代码:

航路点脚本

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;
}
}

看相机脚本

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

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

//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;

// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
if (target)
{
_direction = (target.position - transform.position).normalized;

//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);

//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}
}

我该如何解决这个问题?

最佳答案

假设正在调用 Waypoints.Init() 并且您的 waypoints 变量有一个包含 3 的数组。

  • Waypoints.Init() 启动协程
    • 您的协程等待 3 秒
    • 3 秒后,您将相机目标设置为Slerp朝向该位置
  • Update 在其第一帧显示 waypoints.Length > 0 == true
    • 它没有靠近它的目标,rot 是假的,所以它没有移动

现在,您等待 3 秒,不旋转也不移动。

  • 您的协程的 3 秒等待时间已到,开始转向您的目标
  • rot 现在在轮换开始时为真,因此您的 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");
}
}

看相机

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;
}
}
}

关于c# - 为什么相机不旋转以面向第一个航路点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com