C# 在斜坡上以相同速度移动球-Unity 3D
我想让球在斜坡上移动,就像它在平地上移动一样。目前,当斜坡开始时,球的速度会降低,所以根据玩家的经验,球的整体移动会减少 当它在地面或斜坡上移动时,我想保持相同的速度 虽然下图中我试图解释我的问题: 以下是我使用的代码片段:C# 在斜坡上以相同速度移动球-Unity 3D,c#,unity3d,C#,Unity3d,我想让球在斜坡上移动,就像它在平地上移动一样。目前,当斜坡开始时,球的速度会降低,所以根据玩家的经验,球的整体移动会减少 当它在地面或斜坡上移动时,我想保持相同的速度 虽然下图中我试图解释我的问题: 以下是我使用的代码片段: void FixedUpdate () { if (!GameManager.Instance.IsGameRunninng) { myRigidBody.velocity = Vector3.zero; return;
void FixedUpdate ()
{
if (!GameManager.Instance.IsGameRunninng) {
myRigidBody.velocity = Vector3.zero;
return;
}
if (isJumper) {
isJumper = false;
myRigidBody.AddForce (Vector3.up * 35f, ForceMode.Impulse);
}
isGrounded = Physics.Raycast (rayTransform.position, Vector3.down, 0.5f, groundMask);
Vector3 nextVelocity = myRigidBody.velocity;
nextVelocity.x = ballInputHandler.horizontalInput * smoothnessX;
if (!isGrounded) {
nextVelocity.y -= speed * 0.3f;
} else {
nextVelocity.y = 0;
nextVelocity.z = speed;
}
myRigidBody.velocity = Vector3.Lerp (myRigidBody.velocity, nextVelocity, smoothnessValue * Time.fixedDeltaTime);
ClampingBallMovement ();
}
我希望您正确理解了我的问题,请给我一些建议,这样我就可以解决这个问题。在您调整了
nextVelocity.y
组件后:
myRigidBody.velocity = nextVelocity.normalized * desiredSpeed;
您的问题是
nextVelocity.y=0代码>这里你破坏了你的速度向量的一部分。如果要在斜坡上开始移动时保持水平速度,则必须旋转速度向量。水平速度(5,0)在斜坡上可以变成(4,3)-如果你知道斜坡的角度,你可以用它来得到new.x=old.x*cos(angle)
和new.y=old.y*sin(angle)
图像在哪里?那么我的整体代码会变成什么?我也想使用Lerp,为什么需要Lerp?