- Home /
Multi-lane endless runner. How to toggle lanes?
I have a character which is moving constantly along the x axis, and you can jump altering the y axis velocity, and I don't want the character to be able to move on the z axis, unless you jump left or right.
Everything is working, except when jumping left or right where the character will continue to slide in that direction after landing, and I thought the following would solve this, but there is no noticeable effect.
//when touching a platform i.e. landing the jump
float zVel = rigidbody.velocity.z;
zVel = 0f;
I believe instead of making the character jump using rigidbody.AddForce(...
I should just make the character move in the z axis by a certain distance. I was hoping something along the lines of the following would be possible:
rigidbody.transform.position += (0,0,5);
What is the right way to do this?
Answer by fafase · Nov 09, 2013 at 01:51 PM
Ok here is what is happening:
float zVel = rigidbody.velocity.z;
zVel = 0f;
zVel gets a piece of paper and ask rigidbody to go down to velocity and get the value of z. Rigidbody does so and tells zVel the value there. zvel then writes it on his own paper.
Next line zVel scratches his paper and overwrite 0 on it and goes on with his life. rigidbody was not told about anyhting and z is still the value it was.
What you need is to tell rigidbody to change the value of the velocity:
Vector3 vec = rigidbody.velocity;
vec.z = 0;
rigidbody.velocity = vec;
Here, vec gets the value of velocity on his paper, then overwrites his own value and then tells rigidbody to overwrite his value with the one from vec. Obviously only z has changed.
Please, do not get me wrong, I am not looking down on you using some kind of childish example, it just works on my students (university) to use some real world examples to explain what happens down in memory.
Thanks. I actually copied the code here wrong, but I was making a similar mistake.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Unity Physics On Input Issue? 0 Answers
Movement Script Only Works in One Scene 2 Answers