- Home /
[2D] Angle to Vector conversion
Greetings dear fellows, I've spent 2 hours researching the Internet for the solution. Finally I decided to ask for help here.
My case: in a 2D game and I would like to spawn a projectile inside a player, which will be fired at the direction depending on the way the player is facing.
My assumptions: Because it is a 2D game, with a view from the top of the screen (not sidescroller), the player has 360 degrees to face. I assume it should be done the following way (perhaps, I am wrong): 1) create 0,0,0 vector where 0,0,0 is the middle of the map. 2) calculate the angle a player has while moving 3) create a vector out of this angle 4) normalize this vector (as I understand Normalization helps to clean the vector from unneccessary magnitude information, leaving only direction it is pointing to, like instead of a 0,10 vector we get 0,1 normalized vector. Which is the same direction, but is free of magnitude, am I right?) 5)substract normalized vector from initial 0,0,0 one to get the direction from the object to the middle of the map.
My script so far:
public var rocket : Rigidbody2D;
public var vectorOne : Vector3 = Vector3(0,0,0);
function Update() {
var coordinates = GameObject.Find("professor").transform.position;
var angle2 = Mathf.Atan2(GameObject.Find("professor").transform.position.x, GameObject.Find("professor").transform.position.y) * Mathf.Rad2Deg;
//Debug.Log(angle2);
//Debug.Log(vectorOne);
//Angle - Vector 0.0 = vector distance
//normalize to only get the direction
/* doesn't work
var f = angle2.Vector2.Normalize();
var theNeededVector = f - vectorOne;
Debug.Log(e);
*/
if (Input.GetMouseButtonDown(0))
{
var b: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);
// var force =10;
// b.rigidbody2D.AddForce(theNeededVector * force);
}
}
There are missing pieces of your question, some bad assumptions, at least one bug, and some performance issues. You need to spell out:
What world direction is the camera facing?
Is the player facing the direction you want to fire the projectile when you get the Input.Get$$anonymous$$ouseButtonDown()?
Is the professor object the player you are using for the rotation?
What side of the sprite is considered forward?
As for the clear bug, Atan2 is $$anonymous$$athf.Atan2(y,x), not $$anonymous$$athf.Atan2(x,y);
Atan2, normalize, and so on are for people who already know and use High School trigonometry. But most people don't. If you feel inspired to spend a few months with a textbook, that's great (yes, I'm saying your trig skills seem to be months away from being able to do anything.)
But, as the answer below points out, Unity has built-in stuff to avoid trig. $$anonymous$$ost of the guys ai$$anonymous$$g bullets are just using player.forward*force
. I use it, because it's simpler, and I save trig for the weird stuff.
The trick to searching is, don't look for math. Look for "how to shoot the way I face."
Answer by ajkolenc · Jul 17, 2014 at 11:36 PM
One nice feature that Unity Transform components provide is a few built-in unit vectors (normalized vectors) that point in the directions you might commonly need. So you should be able to use the Transform component of your "professor" object to find the vector you are looking for.
var player = GameObject.Find("professor").transform;
var rocketDirection = player.forward;
Now, this method would rely on you rotating your character to the direction it is facing when you move it, but that is probably a good idea anyway.