- Home /
Converting vector3 velocity to an angel
I have a cannon that rotates on it's Z value. The thing is that I want the character (that is loaded into the cannon) to get a vector3 velocity depending on the angle of the cannon. So how can I convert that angle into a vector3 velocity?
Answer by bateleur · Mar 07, 2012 at 02:47 PM
Typically the easiest way to do this is to use cannon.transform.forward which is a unit Vector3 pointing in the cannon's forward direction.
In your particular case it sounds as though you're doing something odd if your cannon is rotating about the Z axis (which would normally be the facing, to make LookAt work correctly). Consequently you may find you need to reorient your cannon model so that it faces along the forward axis, or alternatively use cannon.transform.right or cannon.transform.up instead as your unit vector depending on which way the cannon model faces.
Of course, the unit vector is unlikely to give you the right launch speed, so you will probably need to multiply it by some scale factor to get the exact launch velocity you want.
Example:
var launchSpeed:float = 3.5;
var myLaunchVelocity:Vector3 = cannon.transform.forward * launchSpeed;
The cannon is actually facing the right side of the screen that's why I have the funky axis orientation. Will try this now and see if it works, thanks!
Answer by Meltdown · Mar 07, 2012 at 12:41 PM
So you want to align your player to the cannon?
You can use Quaternion.LookRotation
Something like this...
var cannonRotation = Quaternion.LookRotation(cannon.transform.right, cannon.transform.up);
cannonRotation.x = 0;
cannonRotation.z = 0;
character.rotation = cannonRotation;
You'll just need to play with the variables to get it right but you get the idea..
Seems my comment didn't get through for some reason. Anyway, the thing is that I want the angle of the cannon to add a vector3 velocity (like player.velocity = vector3(10,10,0) for example) depending on the angle. I don't want to rotate the character just shoot him in the right angle depending on the cannons angle.