为什么物体不朝向另一个物体旋转?
Why the object is not rotating towards the other object?
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;
public class DistanceCheck : MonoBehaviour
{
public Transform target;
public float lerpDuration;
public float rotationSpeed;
public GameObject descriptionTextImage;
public TextMeshProUGUI text;
public ThirdPersonUserControl thirdPersonusercontrol;
private Animator anim;
private float timeElapsed = 0;
private float startValue = 1;
private float endValue = 0;
private float valueToLerp = 0;
private bool startRot = false;
// Start is called before the first frame update
void Start()
{
anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if(startRot)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation,
target.rotation, rotationSpeed * Time.deltaTime);
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "Colliding Area")
{
StartCoroutine(SlowDown());
}
}
IEnumerator SlowDown()
{
while (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
yield return null;
}
thirdPersonusercontrol.enabled = false;
descriptionTextImage.SetActive(true);
yield return new WaitForSeconds(3f);
startRot = true;
}
}
我使用了一个断点,当 startRot 为真时,它确实到达了更新中的 Quaternion.RotateTowards 行并且 rotationSpeed 为 5,但变换根本没有旋转。
我希望变换旋转 180 度,或者变换向前看的平均角度它应该旋转回目标。
变换和目标都面向同一方向,但变换根本没有朝向目标旋转。
如果我自己更改变换旋转,它会在运行时旋转,但当它到达更新时它根本不会旋转。
Quaternion.RotateTowards
将一个旋转方向改变为另一个旋转方向,如果两个物体面向同一方向,则它们具有相同的旋转角度,因此不会发生任何变化。
如果你想让物体面向目标,你需要使用由Quaternion.LookRotation
创建的旋转。
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(target.position - transform.position),
rotationSpeed * Time.deltaTime);
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;
public class DistanceCheck : MonoBehaviour
{
public Transform target;
public float lerpDuration;
public float rotationSpeed;
public GameObject descriptionTextImage;
public TextMeshProUGUI text;
public ThirdPersonUserControl thirdPersonusercontrol;
private Animator anim;
private float timeElapsed = 0;
private float startValue = 1;
private float endValue = 0;
private float valueToLerp = 0;
private bool startRot = false;
// Start is called before the first frame update
void Start()
{
anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if(startRot)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation,
target.rotation, rotationSpeed * Time.deltaTime);
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "Colliding Area")
{
StartCoroutine(SlowDown());
}
}
IEnumerator SlowDown()
{
while (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
yield return null;
}
thirdPersonusercontrol.enabled = false;
descriptionTextImage.SetActive(true);
yield return new WaitForSeconds(3f);
startRot = true;
}
}
我使用了一个断点,当 startRot 为真时,它确实到达了更新中的 Quaternion.RotateTowards 行并且 rotationSpeed 为 5,但变换根本没有旋转。
我希望变换旋转 180 度,或者变换向前看的平均角度它应该旋转回目标。
变换和目标都面向同一方向,但变换根本没有朝向目标旋转。
如果我自己更改变换旋转,它会在运行时旋转,但当它到达更新时它根本不会旋转。
Quaternion.RotateTowards
将一个旋转方向改变为另一个旋转方向,如果两个物体面向同一方向,则它们具有相同的旋转角度,因此不会发生任何变化。
如果你想让物体面向目标,你需要使用由Quaternion.LookRotation
创建的旋转。
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(target.position - transform.position),
rotationSpeed * Time.deltaTime);