- Home /
Imparting Physics in only certain planes of motion
Newbie Unity user and first time poster, hello Unity community!
I have a WoW type camera, where you can move the camera around while it is always focused on one object. Instead of controlling the object directly though, they charge up a hit and release, much like what you would imagine for a mini-golf game.
I would like to allow the user to impart physics onto the ball on only the x and z planes, regardless of whether the camera is looking straight down at the ball from above, from directly under, or anywhere in between.
This is the current code for moving the ball, which allows the ball to be shot into the air.
projectile.velocity = transform.TransformDirection( Vector3(0, 0, power) );
I couldn't find a similar question already asked after a good while of searching, so thank you in advance!
Answer by robertbu · Feb 15, 2014 at 09:39 PM
How about:
var dir = transform.TransformDirection( Vector3(0, 0, power) );
dir.y = 0.0;
projectile.velocity = dir;
Note you can also freeze the 'y' movement in the Rigidbody component of the projectile. In the Inspector, see the 'Constraints' section on the rigidbody.
This worked like a charm! I'm glad the answer was so simple and clean, too. Thank you so much!
Your answer
Follow this Question
Related Questions
Help around an approach to a "Virtual Motion Table" in Unity 0 Answers
How to set velocity to new direction after rotation? 0 Answers
How to apply in opposite direction to transform? 1 Answer
How can i make my crosshair drag behind the camera? 0 Answers
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer