所以,当我松开钥匙的时候,控制器停了下来,就像撞到墙上一样,我试着改变它,但唯一改变的是,每当我按下一个键,它就会被扔到外层空间:
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 newMovement = transform.right * x + transform.forward * z;
momentum = new Vector3(characterController.velocity.x, 0, characterController.velocity.z);
newMovement.y = 0;
if (!newMovement.normalized.Equals(momentum.normalized))
{
Debug.Log("new" + newMovement.normalized);
Debug.Log(momentum.normalized);
momentum = (momentum.magnitude - 2f) > 0 ? momentum.normalized * (momentum.magnitude - 2f) : Vector3.zero;
if (newMovement.x == momentum.x)
momentum.x = 0;
if (newMovement.z == momentum.z)
momentum.z = 0;
}
else
momentum = Vector3.zero;
characterController.Move((newMovement * speed + velocity + momentum) * Time.deltaTime);另外,由于某种原因,即使有时两个向量相等,它们也会通过if语句(我尝试使用!=)(这两个向量都记录在if语句的前2行)。

发布于 2022-06-22 09:28:47
使用https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html,它将逐渐减缓移动到零,这取决于smoothTime的值
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
private Vector3 newMovement;
void Update()
{
newMovement = transform.right * x + transform.forward * z;
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}https://stackoverflow.com/questions/72706921
复制相似问题