Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Liens · Nov 05, 2016 at 03:02 PM · positioningworldspacegraphrotations

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.

WorldToGraph

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.

worldtograph.jpg (21.0 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)
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
avatar image
0

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.

Comment
Add comment · Show 1 · 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 Liens · Nov 06, 2016 at 01:51 AM 0
Share

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

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

58 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

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


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