- Home /
Finding a point relative to an object, based on object's relative position to player
I am trying to find a way to get a point in space relative to an enemy, but based on the location of the player, independent of the rotations of either.
Right now I'm thinking I need a way to transform the world space coordinates into a coordinate system that places the enemy at (0,0) and the player at 0 on the x, and negative on the y. Then I can just take (1,0) and transform that back into world space. Awful hand drawn picture below to help illustrate.
Am I overthinking this, or am I headed down the right path? If I'm on the right track, how does one go about accomplishing this? Preferably without needing a degree in linear algebra :)
Thanks!
Answer by unity_ek98vnTRplGj8Q · Aug 06, 2020 at 07:28 PM
So you want a position directly to the right of the enemy from the perspective of the player? It seems like the easiest way to accomplish this would be to use the cross product. The cross product takes two vectors and returns a vector that is perpendicular to both of them, so if you give the vector from the enemy to the player, and the up vector, you will get your desired vector. The code should look something like this -
public Transform enemy;
public Transform player;
public float distance = 1f;
public Vector3 destination;
void Update(){
Vector3 playerDirection = enemy.transform.position - player.transform.position;
destination = distance * Vector3.Cross(playerDirection.normalized, Vector3.up);
}
void OnDrawGizmos(){
Gizmos.color = Color.Red;
Gizmos.DrawSpehere(destination, 1f);
}
Ok, so I was definitely overthinking it, haha.
Thanks!
Your answer
Follow this Question
Related Questions
Object with many children will not show up in the center although its coordinates are set to 0, 0, 0 1 Answer
probuilder position problem 2 Answers
Position gameObjects in circle around another gameObject 1 Answer
Place object in front of me with given Translation Vector 0 Answers
transform getting altered by changing prefab settings 1 Answer