首页 > 解决方案 > Unity,如何使摩擦与运动一起工作

问题描述

我有一个 3d 世界,它有一个简单的平台,上面有一个代表玩家的立方体。当我旋转平台时,立方体会滑动并按预期执行,从而增加和减少物理材料中的摩擦力。

我希望立方体在输入例如 forward 终止后滑动。它不是。我试图用rigidbody.position 更新位置并更新它。我很快就明白它不适用于物理引擎。

现在我有以下代码。无论如何,它不能按预期工作。我想有一些指针来解决这个问题。

public class Player1 : MonoBehaviour
{
 private float speed = 10f;
 private Vector3 direction;
 private Vector3 velocity;
 private float vertical;
 private float horizontal;


 Rigidbody playerRigidBody;

 // Start is called before the first frame update
 void Start()
 {
    playerRigidBody = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update()
 {
    vertical = Input.GetAxisRaw("Vertical");
    horizontal = Input.GetAxisRaw("Horizontal");
    direction = new Vector3(horizontal, 0, vertical);  
 }

 private void FixedUpdate()
 {
    velocity = direction.normalized * speed * Time.fixedDeltaTime;
    playerRigidBody.MovePosition(transform.position + velocity);
 }
} 

标签: unity3d

解决方案


用于playerRigidBody.AddForce(Vector3 direction, ForceMode forceMode)移动播放器。

如果您不希望您的播放器以痴呆的速度移动,请使用playerRigidBody.velocity = Vector3.Clamp(Vector3 vec3, float minValue, float maxValue);

然后使用不同的变量来获得你想要的结果!


推荐阅读