【发布时间】:2021-06-22 18:41:56
【问题描述】:
我统一做了一个汽车控制器来开车。我读过FixedUpdate 更适合用于物理对象而不是Update,但是当我切换到Fixed 更新时,我的汽车不再行驶。有谁知道这可能是为什么?非常感谢您提供的任何帮助!
public float speed;
public float turnSpeed;
private RigidBody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Move();
Turn();
}
void Move(){
// moves the car
if (Input.GetKey(KeyCode.W)){
rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * speed);
}
else if (Input.GetKey(KeyCode.S)){
rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * -speed);
}
// maintains forward velocity relative to car.
Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
localVelocity.x = 0;
rb.velocity = transform.TransformDirection(localVelocity);
}
void Turn(){
// turns the car.
if (Input.GetKey(KeyCode.D)){
rb.AddRelativeTorque(Vector3.up * turnSpeed);
Debug.Log("TURNING CAR");
}
else if (Input.GetKey(KeyCode.A)){
rb.AddRelativeTorque(-Vector3.up * turnSpeed);
Debug.Log("TURNING CAR");
}
这是代码。按 W 或 S 可增加力,按 A 或 D 可增加扭矩。当我尝试打开 FixedUpdate 时,控制台将按应有的方式写入“TURNING CAR”,这表明它正在通过 AddRelativeTorque 线,但它仍然不会转动,所以我不太确定发生了什么。再次感谢,非常感谢任何帮助。
【问题讨论】:
-
你试过增加力量吗?由于更新比固定更新运行得更频繁,因此在同一时间段内更新的累积力将比固定更新多得多。
-
@hijinxbassist
much more有点夸张,虽然.. 假设 60fps 然后 Update 被调用 60 次,而 FixedUpdate 默认情况下每秒调用 50 次 .. 这不是一个巨大的差异 ;) 但总的来说是的您应该在两者中都考虑Time.deltaTime,然后不应该注意到差异 -
哦,有趣。我会尝试并很快回来
-
成功了!!太感谢了。很高兴这是一个简单的修复。
-
太棒了,很高兴听到这是一个简单的修复(那些是最好的!)。我在下面发布了我的评论作为答案。干杯