Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fritsl · Jun 04, 2019 at 08:06 AM · localpositionlocalrotationglobalspace

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.

Illustration

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 .

skærmbillede-2019-06-04-kl-095954.png (185.8 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Jun 04, 2019 at 08:24 AM 0
Share

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)?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fritsl · Jun 04, 2019 at 08:44 AM 0
Share

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(?,?,?);

avatar image Bunny83 Fritsl · Jun 04, 2019 at 09:44 AM 0
Share

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.

avatar image xxmariofer Fritsl · Jun 04, 2019 at 12:29 PM 0
Share

share how you are moving the enemy so i can help you with the code :)

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I use the local x,y transforms of a plane? 1 Answer

How to have an object some distance above another object Locally? 1 Answer

Changing scale of unit 0 Answers

How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer

transform.localPosition problem. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges