Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 vik.vega · Mar 04, 2014 at 11:31 PM · trigonometryvector3 operations

calculate Vector3 offset relative to surface normal

Hi ! I'm trying to calculate four relative points from a raycast hit point, according to the face normal that was hit.

alt text

Now the offsets in the code are obviously wrongly hardcoded, just to show the intended result, but of course it just works on the plane. I guess what i need, is some trig calculation based on hit.normal to keep the red marked offsets at the same distance from center, according to the face normal that was hit.

 function Update () 
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     
         if (Physics.Raycast (ray, hit))
         {
         Debug.DrawRay(hit.point, hit.normal, Color.green);
         
         Debug.DrawRay(hit.point+Vector3(1,0,1), hit.normal*2, Color.red);
         Debug.DrawRay(hit.point+Vector3(-1,0,1), hit.normal*2, Color.red);
         Debug.DrawRay(hit.point+Vector3(-1,0,-1), hit.normal*2, Color.red);
         Debug.DrawRay(hit.point+Vector3(1,0,-1), hit.normal*2, Color.red);
         }
 }

I can think of a dirty solution like:

  • creating a dummy plane, and keeping it centered to the raycast hit point

  • adjusting it's size to match the desired distance (the red offsets)

  • aligning it with the surface normal it is on

  • then picking my surface-orientation-relative points from the plane i'm using

But i'm actually more interested in understanding the trig that has been passing over my head in the last few days, or even some transformation function I may have missed from the documentation. So, i would really appreciate any hint! Thanks !

raycast_offset.jpg (19.7 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
1

Answer by robertbu · Mar 05, 2014 at 01:16 AM

There are a lot of different ways of solving this problem. One issue is that you have a degree of freedom here you did not define. That is the four red lines may be rotate to any arbitrary angle with the green normal as the axis. If you don't care about that rotation, then a really simple approach is to use an empty game object (with a scale of (1,1,1)). Then you can do:

 empty.transform.position = hit.point;
 transform.up = hit.normal;
 var pos1 = empty.transform.TransformPoint(Vector3(1,0,1));
 var pos2 = empty.transform.TransformPoint(Vector3(-1,0,1));
 var pos3 = empty.transform.TransformPoint(Vector3(-1,0,-1));
 var pos4 = empty.transform.TransformPoint(Vector3(1,0,-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 vik.vega · Mar 05, 2014 at 01:29 AM 0
Share

Thanks for your time, robertbu!

You may have missed the part where i already thougth about using an empty object, aligning it, and using it's points to cast rays.

What I'm interested in understanding, is how to achieve this, working with the vectors i have, being hit.point and hit.normal

But you're right, I may have not defined completely the conditions: I want the lines to be parallel to hit normal, "sliding" over the surface with a given offset.

avatar image robertbu · Mar 05, 2014 at 02:24 AM 0
Share

You may have missed the part where i already thougth about using an empty object, aligning it, and using it's points to cast rays.

Didn't miss it, but thought that you were talking about a real object of some sort rather than an empty game object with the reference to 'resizing'. Let me throw out another solution. For it to work you need either 1) one additional point on the plane, or 2) any vector that is not the same as the normal (and can be generated randomly). Using the second, you can do:

 var dir = Vector3.Cross(hit.normal, otherVector).normalized;
 var pos1 = hit.point + dir;
 var pos2 = hit.point + Quaternion.AngleAxis(90,hit.normal) * dir;
 var pos3 = hit.point + Quaternion.AngleAxis(180,hit.normal) * dir;
 var pos4 = hit.point + Quaternion.AngleAxis(270,hit.normal) * dir;

Or this should work:

 Quaternion q = Quaternion.FromToRotation(Vector3.up, hit.normal);
 var pos1 = hit.point + q * Vector3(1,0,1);
 var pos2 = hit.point + q * Vector3(-1,0,1);
 var pos3 = hit.point + q * Vector3(-1,0,-1);
 var pos4 = hit.point + q * Vector3(1,0,-1);
avatar image vik.vega · Mar 05, 2014 at 02:34 AM 0
Share

yes ! the second solution is more like it ! :) $$anonymous$$aybe since the raycast is from the camera, camera transform could be taken into account. Anyway thanks again, i'm testing your hints right now !

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

20 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

Related Questions

How to get the angle between an object and a plane based on the plane's normal? 3 Answers

Change orientation / rotation of NavMeshAgent, Trig question? 0 Answers

Check if object is inside a viewing radius 1 Answer

LineRender and Gizmos.DrawRay are showing different results for the same input values 0 Answers

Finding the center of a rotating image. 1 Answer


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