- Home /
Finding a local position matching a global position in an individually rotated hierarchy
I know this is a "classic" question, but I still cannot get my head around it.
I have boiled my question down to the attached illustration, and would greatly appreciate if someone can help. For the sake of Google, other people who should search this, here are some keywords: I want to find the local position matching a global position in an individually rotated hierarchy .
i think i can help you but i am having some issues understanding your question, you want the enemy to be at the same "level" for blocking others path, independent of enemys rotation (and enemys parent rotation)?
Answer by Bunny83 · Jun 04, 2019 at 08:11 AM
Your question is quite abstract. However just to find the world space position for a local space position in a given local space, all you need is TransformPoint.
However if you just want to know the world space position of a nested object, just read it's position property which does this conversion automatically.
edit
I just realised you want to opposite ^^. In this case you want to use InverseTransformPoint or again if you just want to know the local space position of an object within its parent space, read localPosition
second edit
Ahh I think you worded your question in a very confusing way. From your strange line of code I think you actually want to project the other objects position onto the patrolling line. If the patrolling line is always the z axis of the parent space of your evil guy, yes using InverseTransformPoint of that parent object passing the position of the player and then reading the z component would work.
Note your code examples are barely visible due to contrast. When you provide code examples, please include the code in your question and don't post screen shots / snippets of code.
Hi, sorry you find it abstract / confusing, I was trying hard to be clear ;)
I want the evil guy to move on a line: His local Z I want him to match and meet the other, when he is passing the line, right where the paths cross.
I am very much aware of the inversetransformpoint, dot and whatnot ;P Linking to that is not much of a help, I need to change this line:
EvilGuy.transform.position = new Vector3(EvilGuy.position.x, EvilGuy.position.y, other.transform.position.z);
to something where the "(?,?,?)" is replaced with the right code - links to Unity documentation does not help, code does. Thanks :)
EvilGuy.transform.position = new Vector3(?,?,?);
Like I said in my second edit, InverseTransformPoint and using the z component should just work fine
Vector3 playerInEnemySpace = enemy.transform.InverseTransformPoint(player.transform.position);
Vector3 localSpaceTarget = new Vector3(0, 0, playerInEnemySpace.z);
Vector3 worldSpaceTarget = transform.TransformPoint(localSpaceTarget);
"worldSpaceTarget" would be that projected point you were talking about.
Though note that this only considers the position of the player projected onto the patrolling line of the enemy. Of course it does not take into account the movement direction of the player. It's essentially the closest point on the patrolling line towards the player.
I have the feeling you're actually interested in calculating the interception point for the enemy. However this means you need to actually calculate the intersection point between the two rays (your enemy's patrolling line and your players movement direction). For this see the LineLineIntersection method given here. You would use it like this:
Vector3 target;
LineLineIntersection(out target, player.position, player.forward, enemy.position, enemy.forward);
This will give you the intersection point of the two lines in world coordinates.
share how you are moving the enemy so i can help you with the code :)
Answer by xxmariofer · Jun 04, 2019 at 12:41 PM
ill post it as an answer since i think you are talking about something like this
if (evilGuy.position.y < other.position.y) evilGuy.position += evilGuy.up * speed * time.deltaTime;
else if(evilGuy.position.y > other.position.y) evilGuy.position -= evilGuy.up * speed * time.deltaTime;
Your answer
Follow this Question
Related Questions
Please help me convert this script from using position to rotation? 0 Answers
Locals How to get rid of them can I disable them??? 1 Answer
Help me convert this script from using position to rotation? (BIG PROBLEM!) 2 Answers
rotated position, together with the object (around the origin) 0 Answers
Z-axis relative to an object. 2 Answers