- Home /
Question by
Attapi0 · Feb 15, 2014 at 04:01 PM ·
timemovement scriptframeratedeltatime
I can't get framerate independence to work...
So I have a movement script for a rigidbody. I use Time.deltaTime for everything but it definitely isn't framerate independent. Lower framerates make you move faster, and higher framerates make you move slower. Check out my code.
#pragma strict
var MovementSpeed = 7000;
private var MovementVer : int;
private var MovementHor : int;
private var distGround : float;
var MaxSpeed : float;
var jumpSpeed : float;
function Start(){
distGround = collider.bounds.extents.y;
}
function IsGrounded() : boolean {
return Physics.Raycast(transform.position, -Vector3.up, distGround + 0.1);
}
function Update() {
//setting up the movement variables
MovementVer = Input.GetAxis("Vertical") * MovementSpeed * Time.deltaTime;
MovementHor = Input.GetAxis("Horizontal") * MovementSpeed * Time.deltaTime;
//Stops the character from sliding around when you aren't moving
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) {
rigidbody.drag = 1;
}
else{
rigidbody.drag = 10;
}
if(Input.GetAxis("Jump") && IsGrounded()){
rigidbody.drag = 0;
rigidbody.velocity.y = jumpSpeed;
}
if(IsGrounded() == false){
rigidbody.drag = 0;
}
if (IsGrounded() == false){
MovementSpeed = 2;
}
if (IsGrounded() == true){
MovementSpeed = 7000;
}
}
function FixedUpdate () {
rigidbody.AddRelativeForce(Vector3(MovementHor, 0, MovementVer));
}
Comment
Best Answer
Answer by whydoidoit · Feb 15, 2014 at 04:04 PM
You are moving the item in physics FixedUpdate but you are using Time.deltaTime to work out the horizontal speed when it needs to be Time.fixedDeltaTime because of where you actually apply the movement.