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 SmonSvk · Feb 15, 2019 at 11:33 AM · distancelocalspace

Distance with Vector3.Project

Hello, I'm trying to get distance between gameobject and camera in local space of green object. But I can't make it work.

alt text

I tried this code:

 Vector3 dist = transform.position - green.transform.position;
 
 Vector3 distanceZ = Vector3.Project(dist, green.transform.forward);

This code return same values as before projection. I also tried .right normal but it return some weird values.

Thank anyone for help because I was trying to make this to work for few hours with no luck.

projection.png (5.9 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

1 Reply

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

Answer by Bunny83 · Feb 15, 2019 at 12:49 PM

It's not entirely clear what you actually need. The distance is just a scalar value. Unless the local space of the green object is scaled in a strange way, the distance of an object is the same in world or in global space. When you projecting a vector, the actual distance changes since, depending on the vector orienation, some part of it may be removed.


Your drawing is actually correct for what you're doing. However your "distanceZ" is actually a world space vector that points along the normal vector you used (the forward direction of the green object). To determine that projected distance you need to use distanceZ.magnitude which is the distance of the camera from the imaginary plane defined by the green object and its forward vector as normal.


Note that the forward, up and right vectors of a gameobject do not represent the local space of an object. Those are just the normalized direction vector of that object's local space, but defined in world space. It there is no scaling going on, they are the same as the basis vectors of the green object's local space.


If you really want the distance vector between the green object and your camera in local space of the green object, you can simply use InverseTransformPoint of the green object and pass in the position of the camera. The resulting vector is the local space position of the camera as seen from the local space of the green object. So the z component is the same as the whole magnitude of your "distanceZ" vector.

 Vector3 camLocalPos = green.transform.InverseTransformPoint(transform.position);
 float dist = camLocalPos.z;

Note that in this case (assuming your drawing) the z value would be negative, since your camera is "behind" the object (or in a negative z octant)


Finally note that i've actually assumed that the pivot of the green object is the center (since you draw the "plane line" through the center). If the pivot is where you've drawn your coordinate axis, you're missing information

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 SmonSvk · Feb 15, 2019 at 01:14 PM 0
Share

Thank you. InverseTransformPoint did what I need.

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

99 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

Related Questions

How to use Sphere colliders to detect the "Player" 1 Answer

Vector3.Distance not working properly? 3 Answers

Resize Array based on distance. 2 Answers

don't include vertical distance? 2 Answers

Activate Object in a Certain Distance 2 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