- Home /
World position to graph position
I'm having an issue where I want to map a world position onto a 2D graph relative to where an object is looking.
The graph bounds are -1x to +1x and -1y to +1y. You could think of it as a kind of targeting reticle or something, except it has nothing to do with screen position.
This is what I've got so far:
Vector3 dirToTarget = Quaternion.LookRotation(target.position - transform.position) * Vector3.forward;
Vector3 headingDir = Quaternion.LookRotation(transform.forward) * Vector3.forward;
float difference = Vector3.Angle(dir, headDir) / 90f;
Vector2 finalPos = dir * difference;
Where I get the direction to the target, the direction this object is facing, and the result is the difference between the two (if this GObj rotates to face the target directly it should be graph position 0,0) finalPos is the position on the graph.
However it doesn't seem to be working correctly, and I can't figure out why.
Answer by Liens · Nov 06, 2016 at 04:04 AM
Ok so I just needed to compensate for the GObj's own rotation by translating the direction it was facing into local space.
Vector3 dir = transform.worldToLocalMatrix * transform.forward;
Then I had to negate Y to map it onto the 2D plane.
new Vector2(dir.x, -dir.y)
Answer by Bunny83 · Nov 05, 2016 at 06:20 PM
You're doing some really strange things there. First of all this:
Vector3 dirToTarget = Quaternion.LookRotation(target.position - transform.position) * Vector3.forward;
is the same as
Vector3 dirToTarget = (target.position - transform.position).normalized;
and this:
Vector3 headingDir = Quaternion.LookRotation(transform.forward) * Vector3.forward;
is the same as
Vector3 headingDir = transform.forward;
It's not clear what "dir" and "headDir"actually is. Why do you "scale" the dir vector by the angle difference between the two vectors? So if the angle is larger the vector will get longer. Vector3.Angle returns a value between 0 and 180 so "difference" is in the range of "0" to "2".
You talk about displaying a "world position" but you only use relative vectors. Your question is confusing and not clear.
I appreciate your interest in the question, however this most likely should have been a comment and not an answer.
I'm using LookRotation because .normalized computes with a square root which is computationally slow. But I'm not sure if LookRotation is any faster. I re-read what I wrote and I probably messed up the code I wrote into the question, I was pretty tired sorry about the confusion.
The angle scale thing is just my attempt at modifying the vector by the angle. What I was actually trying to do is get the difference between dirToTarget and headingDir, because if the difference is zero then that means the GObj is looking directly at the object so the resulting graph point should also be zero (0,0). I realise it's from 0 to 180, I used / 90f because 1 is the maximum and I want it to hit 1 at 90 degrees since I'm not interested in plotting to the 'graph' infront of the GObj when the target is behind the GObj (angle difference is greater than 90). I've clamped it to 1 inside the 'graph' shader so it doesn't matter if it goes to 2.
The world position is target.position, it's in worldspace. As is transform.position.
Your answer
Follow this Question
Related Questions
ViewportToWorldPoint returns same value regardless of input 1 Answer
How to fix this shader glitch? 0 Answers
Endless racer - positioning of track segments 0 Answers
How to position UI elements on the Minimap's rectangle relative to the elements target position? 1 Answer
Ideas on enemy unit formations solution 0 Answers