- Home /
Changing 1 parameter of rigidbody.velocity (Vector3)
Hello!
I've got this line of code: rigidbody.velocity = new Vector3(0, 10f, 0);
It almost works as intended. However, it's only supposed to change the Y value (10f).
How can I make the X and Z value be unchanged, while changing the Y value?
Answer by GerryM · Feb 06, 2013 at 08:08 PM
To be sure x and z velocity don't change, get the old velocity values and pass them on:
rigidbody.velocity = new Vector3 (rigidbody.velocity.x, 10f, rigidbody.velocity.z);
amazing solution:
yVel = rb2d.velocity.y; rb2d.velocity = new Vector2(Input.GetAxis("Horizontal"), yVel);
The problem is, it is getting the velocity (for me Y) and setting it, every frame, however the script is getting the velocity (for me y so i'm going to be saying it's y) let's say 1, so it sets it to 1, great right? wrong, gravity is constant, however the object is going from static to moving, so gravity accelerates the object, however the code rigidbody.velocity = new Vector2 (10f, rigidbody.velocity.y);
will keep the y velocity constant, not allowing for the previously mentioned acceleration. to fix this, I made a variable, specifically for the Y velocity, but it gets set before the velocity does (it's in the Update method), so the acceleration can happen.
I had a major problem with this and it was a Eureka moment xD
Answer by Berenger · Feb 06, 2013 at 12:40 PM
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-constraints.html.
Although, your change of the velocity should not change the x or z of the object, unless there is another force than gravity and initial velocity. Constant Force, another object making the first one bounce ?
Your answer
Follow this Question
Related Questions
Slow down a character while they are midair while keeping their original velocity 0 Answers
Rigidbody character controller can't walk on stairs 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Character Joints and Animations results in strange physic animation 1 Answer
Rigid Body Velocity max 1 Answer