- Home /
Is time-based update necessary in FixedUpdate?
Hi everyone, given the simple control script
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private float x;
private float z;
public float speed=100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
x= Input.GetAxis("Horizontal")*speed*Time.deltaTime;
z= Input.GetAxis("Vertical")*speed*Time.deltaTime;
rigidbody.AddForce (x,0,z);
}
}
I wonder if multiplying by Time.deltaTime is really necessary when applying forces in FixedUpdate. Shouldn't FixedUpdate already be time-based and not frame based?
Can you clarify this point for me? And plus, do you have any advice for how to improve my code?
Thank you!
Another question on the very same script. I used it on a cylinder moving across a field (it is suspended in the air, without gravity influence). Four static cubic colliders act as walls, defining a simple rectangular bound.
The cylinder has a capsule collider and a physics material with bounciness 1. It has a small mass (0.25) and a little drag (0.01).
Problem is, with speed set at 50, the collider often fails, making the cylinder pass through the wall. I tried to divide the fixed timestamp by half, with no result. The object is quite fast, but it's certainly not a bullet (it is meant to be controlled by the player), so these errors seem quite strange to me.
Is there anything else I can do to improve the collision detection? I tried to set the rigidbody parameter as Continuous Dynamic, with no results whatsoever.
Answer by Dave-Carlile · Feb 05, 2013 at 01:35 PM
It's not strictly necessary, but if you don't do it your speed needs to be in units per fixed frame, rather than units per second. I personally always scale by time so my units can be expressed per second. Also, if you don't scale by time and ever decide to change the fixed update rate then you have to go and modify all your rates if you want everything to continue moving the same speed.