- Home /
velocity direction and reversing it
hey guys, i'm in the middle of making a snowboarding game where I want the velocity of the snowboard to be in the direction it is facing. The way I'm trying to do this is by saying "if the velocity direction is not the snowboard direction, then add a force in the opposite direction of the velocity to counter it". This is what I have so far(but it's not working):
if(rigidbody.velocity.normalized != transform.eulerAngles){
var velocityDirection = transform.InverseTransformDirection(rigidbody.velocity.normalized.x, rigidbody.velocity.normalized.y, rigidbody.velocity.normalized.z);
rigidbody.AddForce(velocityDirection * rigidbody.velocity.magnitude);}
I would appreciate it if you guys could help me out with this one. Thanks!
Seems like using hills and a rigidbody would be a simple solution to avoid coding things like this. Seeing how snowboarding is all physics driven, why not let the physX engine take care of it?
Guess he wants the effective friction and board shape to be like a boats keel and convert the lateral movement into forwards movement.
Answer by whydoidoit · Jun 13, 2012 at 09:16 AM
How about:
void FixedUpdate()
{
rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
}
Thanks $$anonymous$$ike! That works, but it seems to be a little sensitive. For example, my "forward" is supposed to rotate 180 when the board flips direction. So when I play the game, I get immediate changes of direction when I start to ride switch(when my back foot is in front of my front foot). Is there any way to make it any less sensitive?
Hey mate - sorry your "answer" was in moderation - btw on Unity Answers you should use "Add New Comment" for this kind of thing as it isn't a "solution". There's a link hidden on the right and comments don't need moderation.
To make this happen over time you could do this:
rigidbody.velocity = Vector3.Lerp( rigidbody.velocity, transform.forward * rigidbody.velocity.magnitude, Time.deltaTime / 1); //Change /1 to fit your preferred speed of change, the bigger it is the slower
Huh, where did my comment go?
I was mentioning that transform.forward * rigidbody.velocity.magnitude
will keep the velocity the same, even when doing extreme swerving motions. For a more realistic approach (i.e., slowing down if you turn by a large angle), use Vector3.Project(rigidbody.velocity,transform.forward)
.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072709im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody Local Velocity 1 Answer
How to move rigidbody object towards a target object 0 Answers
How to use local angular velocity? 1 Answer
NSEW Direction of Rigidbody Velocity 3 Answers
Getting Turret to Fire on the Correct Top-Down Plane 0 Answers