- Home /
How to get the angle between two objects with OnTriggerEnter2D
Hi, I'm making a 2D top down space shooter and I'm trying to get the angle between the projectiles that my ships fire and the object (which is a trigger) that they enter. This is because in my game I have shield facings and need to determine which facing to deduct hit points from.
I'm trying to use this:
void OnTriggerEnter2D(Collider2D other) {
Vector3 targetDir = other.transform.position - gameObject.transform.position;
Vector3 forward = transform.up;
float angle = Vector3.Angle(targetDir, forward);
Debug.Log (angle);
}
But the numbers I'm seeing are all over map and not at all usable. Even if the numbers were good, this method seems to give no way to distinguish between the left and right facings (eg. if it actually worked, getting hit on either side would return something around positive 90 degrees)
This would likely be a simple problem if I were to use rigid bodies, but I am not using rigid bodies and am not able to. Instantiating the projectiles as rigid bodies seems to a cause a giant host of issues, including (though not limited to) a massive amount of unsightly "judder" or "hitching".
Any help would be appreciated, thanks!
Okay, so I have this more or less figured out. Robertbu was more or less right, but was missing one little piece. "transform.InverseTransformDirection(dir);" needs to be "other.transform.InverseTransformDirection(dir);"
The correct code is as follows:
Vector3 dir = other.transform.position - transform.position;
dir = other.transform.InverseTransformDirection(dir);
float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
Robert, thanks a bunch. If you edit your solution to the above I'll accept it as correct.
Edited. I guess I did not understand which object needed the relative angle.
Answer by robertbu · Jun 16, 2014 at 09:25 PM
Given this is 2D, Atan2 should work. Using Transform.InverseTransformDirection() will make the angle relative to the game object rather than a world direction:
Vector3 dir = other.transform.position - transform.position;
dir = other.transform.InverseTransformDirection(dir);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
I believe the angle you get back will be -180 to 180 based on Vector3.right as the 0.0 angle. I see you are using transform.up as your current forward, so you can either normalize the result to using up, or you rotate your sprite so that forward is right.
This seems to be closer to what I'm looking for but the exact implementation you've set given seems to have a few issues. It does return from -180 to 180, but there are two points on a 2D circle collider where it will return each value. For example, without normalization firing on either the leftmost or rightmost side of the collider returns -90, front and back return 90, 0 occupies two of the diagonals with 180 for the other two.
Did you catch my edit to this code. It needs to be InverseTransformDirection() not TransformDirection().
I have now that you've pointed it out, but the modification seems to make things worse. The numbers returned now seem like junk. Never negative, and nothing ever close to 0. Looks like between 40 and 150 and the only pattern I can see is that directly in front, behind, left, and right all return 90.
Atan2 is pretty reliable, but garbage in, garbage out. I would use debug to take a hard look at the values getting passed in (dir.y, dir.x). There's no reason why opposite angles should result in the same output - but there's plenty of ways they can end up looking that way.
Well, the only two inputs are a simple circle collider for the object and a box collider for the projectile. The only manipulation being done on either is for the projectile to move forward via transform.Translate. I don't see any place for 'garbage' to get in the way, so I'm thinking the problem is somewhere in these two lines:
Vector3 dir = other.transform.position - transform.position;
dir = transform.InverseTransformDirection(dir);
Answer by allformegrog · Jul 31, 2017 at 05:13 AM
As of version 2017.1 there is a Vector3.SignedAngle function that looks like it will do as you need.
https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html
Answer by thomasindustry · Jun 16, 2014 at 09:24 PM
The Mathf.Atan2 function might give you better angles
Your answer
Follow this Question
Related Questions
Finding the angle between 2 clicked points 2 Answers
How do i get a attack offset of the player at a fixed distance in 2D? 1 Answer
Drag and jump mechanics? 2 Answers
How do I fire a 2D projectile based on it's current angle/location. 1 Answer
follow an object's x-axis and have it translate smoothly into another object's z-axis rotation 1 Answer