- Home /
Get the angle of a gameObject or correct rotation
Hello,
I need to get the angle of a gameObject so i can use it to shoot a projectile in the same direction.
Vector2 force = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)) * bulletSpeed;
projectile.rigidbody2D.velocity = force;
This is the snippet I plan to use to shoot the newly created projectile and when i enter a angle, like 90, 45, 270 or some other value it seems to fire correctly.
Now I "just" have to get the angle automatically from an object named spawn that is the child of a object that rotates (so I guess local coordinates/angels won't work).
How would I go about doing this? I have been at it for hours now..
Thanks in advance!
Answer by Jeff-Kesselman · Jun 01, 2014 at 11:02 PM
transform.forward is a normalized vector pointing down the local Z axis.
Cheers, that seems a lot simpler!
I did have to use transform.right since I don't have the blue axis but it seems to do what I want.
However when my character faces left it still shoots to the right, it is the correct angle but in the wrong direction.
I guess this has something to do with the movement script that I use (the sample assets from unity). this is what it does then you turn:
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// $$anonymous$$ultiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Is there a way to counter this effect for this calculation? Or is there anything else that is messing around?
the rotating object (the arm) and the empty spawn object are children of the character that this is used on.
Here are some images to explain this more clearly:
So, is there a way to get the red axis stay in the correct angle? even when its parent is flipped around.
Otherwise I have to make 2 separate animations for every action so I don't flip around, but I was hoping to save me that hassle. cheers!
Seems to be a bug/"feature" in unity, simply don't flip the scale when dealing with children. make separate sprites for left/right =(
Your answer
