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 Wolfrik_Creations · Oct 03, 2016 at 10:46 PM · c#raycastvector3vector2localspace

Get RaycastHit Local Coords (C#)

First of all, if you use JS and not C#, I know them both very well and can convert it, as I used JS for around a year before switching to C#.

Now, I know this is probably a really stupid question, but I need to convert RaycastHit.point to a local space ON an object. I don't exactly know how to explain this, so I make a simple example picture.

Here's the link, Unity Answers was being an ass and wouldn't let me put the picture even though it was a smaller file

So, if anyone could please tell me how to have this returned as a Vector2, or Vector3 if needed, then thank you!

I imagine it's something to do with its current position and its scale, but I'm unsure of exactly what to do, this is probably something very very easy.

Thanks in advance!

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
1
Best Answer

Answer by Bunny83 · Oct 04, 2016 at 12:54 AM

Well, to transform a world position into a local position you can simply use "InverseTransformPoint" of your target object. So the resulting point is relative to the target objects space. This is actually the reverse method of TransformPoint which transforms a localspace position into worldspace.

However, keep in mind that localspace might be something different then what you think. What you've drawn looks like texture / UV space. However if the origin of that plate is at that 0, 0 point it should work if the object has a unscaled size of "1".

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 Wolfrik_Creations · Oct 04, 2016 at 12:59 AM 0
Share

That is why I said that it will probably have something to do with the scale, so you could do whatever divided by the scale.

I will give this a test and reply back in about an hour when I can, thank you!

avatar image Bunny83 Wolfrik_Creations · Oct 04, 2016 at 11:10 AM 0
Share

TransformPoint as well as InverseTransformPoint will take into account the objects position, rotation and scale. That's what a "space" is made of.

For example Unity's default cube mesh has a size of 1.0 in each direction but the pivot is in the center so the corners are at +-0.5 in each direction. If you scale the cube up so it's 5x5x5, the local points of the corners are still at +- 0.5

We don't know how "large" your object is in local space. If it's 3x0.1x4 you would need to divide the x position by the x-size (3 in this case), the y position by the y-size (0.1 here) and the z position by the z-size (4) to get a range of "0 - 1"

If your pivot is not at one of the corners you need to offset the position. So if the pivot is in the center (like the default cube) you would simply add 0.5 to each axis after you divided by the size.

Note: size and scale are two completely different things. Size can mean the actual size of the object either scaled or unscaled. The scale is just a multiplication factor.

If you don't know the size of the object you can use $$anonymous$$esh.bounds of your object's mesh. It gives you the local space bounding box of the mesh.

It's generally easier to answer a question when we know what this all is about (so what's the actual goal). Abstract questions can only receive abstract answers.

avatar image Wolfrik_Creations Bunny83 · Oct 04, 2016 at 05:29 PM 0
Share

Thank you!

Transform.InverseTransformPoint worked perfectly!

The origin is at the center, but that's actually better.

The goal was a locking system for structure placing, but I needed to check what corner you are looking at for pillars, what side for walls and other floors, etc.

The chunk I used to test it, if anyone needs it.

         if(lookingAt.transform.gameObject.name == "Floor_type{STRUCTURE}"){
             Vector2 point = lookingAt.transform.InverseTransformPoint(lookingAt.point);
             print(point);
         }

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

236 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 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 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

Snapping object to raycast hit in 1 local axis. 1 Answer

Get a position relative to a gameobject 0 Answers

Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers

Get forward direction of face hitted by Raycast. 1 Answer

Roll ball towards Vector3 point (Rigidbody) 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