- Home /
How to calc new coordinates of an object based on rotation
I'm trying to make a radar like thing where all the enemies are shown relative to my player on a display. (just like in the old elite) I have it all working properly until I rotate the player. I need to calculate the new coordinates of the enemies when the player rotates. for example: (assume 2d)
The player is at 0,0 and a enemy is at 0,6 so in front of the player and showing up in front of the player on the radar. now I rotate the player left 90 degrees, I need to turn the enemy real world coordinate into a new one based on the players new rotation so it shows up to the right of the player on the radar so in this example the coordinates would be 6,0
Any ideas?
Answer by troien · Nov 12, 2014 at 02:45 PM
The easiest way to do such thing is to actually just add a extra camera who looks at your player top-down (Who is a child of your player so that it rotates along). And make it render a different layer, and put all minimap/radar associated things on that layer. This is probably easier then calculating and positioning everything yourself, as you can simply add something to your prefabs in that layer, and as long as your main camera doesn't render that layer, only your minimap will be able to see it.
As for you question. What you wan't is the local position to the player. You can get that using transform.InverseTransformPoint.
player.transform.InverseTransformPoint(other.position);
The result of that would be a position relative to the player.
Thanks. That worked perfectly! I couldn't use your camera suggestion as the radar is on the 2d hud and is an ellipse to make it look 3d, so I'd not be able to put the output of the other camera on it and make it fit and look right. Doing it myself is more useful too as I can add stuff to the radar that doesn't exist in the world. Thanks for the inverse transformpoint idea tho!
Your answer
Follow this Question
Related Questions
transform position y changes when character rotates 0 Answers
Transform a Vector by a Quaternion 1 Answer
How to ROTATE an object without slowing the ends (lerp) 3 Answers
Smooth transform position and rotation 1 Answer
Following another object's position/rotation like parent/child relationship? 4 Answers