unity3d - 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);
}
}
解决方案
用于playerRigidBody.AddForce(Vector3 direction, ForceMode forceMode)
移动播放器。
如果您不希望您的播放器以痴呆的速度移动,请使用playerRigidBody.velocity = Vector3.Clamp(Vector3 vec3, float minValue, float maxValue);
然后使用不同的变量来获得你想要的结果!
推荐阅读
- sql - SQL 员工/公司/国家
- artifactory - 如何自动接受 Artifactory EULA?
- java - 在保持代码干净的同时重用准备好的语句的最佳实践?
- ms-word - document.contentControls 不返回文档中的所有 ContentControls
- typescript - 打字稿:如何制作接受键匹配通用但所有值都是值参数的映射函数的对象的类型
- date - 在谷歌表格公式中添加迄今为止的持续时间
- mongodb - 如何查询以获取基于一个字段作为用户名引用到另一个集合的所有文档
- ios - 在创建 Observable/Single 时获取传递给 subscribeOn() 的调度程序
- ruby-on-rails - Rails 验证错误消息中的插入符号有什么作用?
- php - 使用 PHP 获取签名的 id_token