public class DampAngle : MonoBehaviour
{
public Transform target;
float smoothTime = 0.3f;
float distance = 5.0f;
float yVelocity = 0.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref yVelocity, smoothTime);
Vector3 positons = target.position;
positons += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, distance);
transform.position = positons + new Vector3(0.0f, 2.0f, 0.0f);
transform.LookAt(target);
}
public void OnGUI()
{
if (GUILayout.Button("target旋转60°"))
{
target.eulerAngles += new Vector3(0.0f, 60.0f, 0.0f);
}
}
}
脚本付给一个立方体,target拖一个球体进去,结果立方体会在球体两端疯狂移动。
yAngle计算出来是89.89→281.2504→96.795→278.78……
如果在distance前加“-”,就能正常运行,yAngle也是类似-2.421408E-07这种小的数字,目前找不到原因,求助各位大佬。