//Movement
var walk : float = Input.GetAxis ("Vertical") * walkSpeed;
if (Input.GetKey (KeyCode.W)) {
rigidbody.MovePosition (transform.position + transform.right * walk * -1 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S)) {
rigidbody.MovePosition (transform.position + transform.right * walk * -1 * Time.deltaTime);
}
What am I doing wrong? Thanks in advance.
What happens if you change the + into - and remove that “-1”?
I tend to put parenthesis around the ‘-’ on those calculations, to be really sure that it does not interpret it as a subtraction.
Not saying it will change a thing, but it might.
slay_mithos:
What happens if you change the + into - and remove that “-1”?
I tend to put parenthesis around the ‘-’ on those calculations, to be really sure that it does not interpret it as a subtraction.
Not saying it will change a thing, but it might.
Thanks for the tip but unfortunately you were right, it didn’t change anything.
hpjohn:
Update or FixedUpdate?
Moving it to FixedUpdate solved the the problem. I used this code.
function FixedUpdate () {
//Movement
var walk : float = Input.GetAxis ("Vertical") * walkSpeed;
if (walk > 0 || walk < 0) {
rigidbody.MovePosition (transform.position - transform.right * walk * Time.deltaTime);
}
}
Rigidbodies get updated on FixedUpdate. You register them to be updated in Update, tho they will carry the old position untill a FixedUpdate kicks in, updating the position to the one last set by an Update. Make sure to handle anything related to rigidbodies in FixedUpdate in the future.