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 iacopoooo · May 25, 2017 at 02:57 PM · raycastcursormeshcollider3d model

Make a cursor following 3D surface in Unity 3D

Hello,

I need to move a cursor that always follows the 3D mesh of an object imported in unity. This cursor should move with a trackpad or with arrow keys but independently from the mouse screen position. I was able to made it using a raycast starting from an empty object (the one that is moved as defined previously) and calculate the normal to the surface. When this change, the empty object is correctly oriented (z axis normal to surface) and moved (distance on z axis constant). It seems to work correctly but I have problems when the surface angle change for more than 90 degrees, in this case in fact the raycast lost the hit point or can't move on. Does anybody know which can be the solution of this problem or an alternative approach? Thank you very much!

Here the script I'm using:

         Ray![alt text][1]castHit hit;
         Ray ray = new Ray (cursor.transform.position, cursor.transform.forward);
         if (Physics.Raycast (ray, out hit)) {
             Vector3 incomingVec = hit.point - obj.position;
             Vector3 reflectVec = Vector3.Reflect (incomingVec, hit.normal);
             Vector3 projectVec = Vector3.ProjectOnPlane (incomingVec, hit.normal);
             Vector3 normalVec = hit.normal;
 
             Vector3 uvWorldPosition = Vector3.zero;
             MeshCollider meshCollider = hit.collider as MeshCollider;
             Vector2 pixelUV = new Vector2 (hit.textureCoord.x, hit.textureCoord.y);
             uvWorldPosition.x = pixelUV.x - canvasCam.orthographicSize;//To center the UV on X
             uvWorldPosition.y = pixelUV.y - canvasCam.orthographicSize;//To center the UV on Y
             uvWorldPosition.z = 0.0f;
             cursorC.transform.position = uvWorldPosition;
             float RX = Mathf.Acos (normalVec.x) * 180f / Mathf.PI;
             float RY = Mathf.Acos (normalVec.y) * 180f / Mathf.PI;
             float RZ = Mathf.Acos (normalVec.z) * 180f / Mathf.PI;
             float XX = normalVec.x * 65;//hit.distance;
             float YY = normalVec.y * 65;//hit.distance;
             float ZZ = normalVec.z * 65;//hit.distance;
             float angle = Vector3.Angle (normalVec, incomingVec); 
             //normal position
             cube.transform.localPosition = new Vector3 (XX + hit.point.x, YY + hit.point.y, ZZ + hit.point.z);
             cube.transform.forward = -hit.normal;
             cursor.transform.position = cube.transform.position;
             cursor.transform.eulerAngles = cube.transform.eulerAngles;
         }
     }
 
     void move ()
     {
         if (Input.GetKey ("up")) {
 
             cursor.transform.Translate (Vector3.up * Time.deltaTime * 100);
         }
 
         if (Input.GetKey ("down")) {
 
             cursor.transform.Translate (Vector3.down * Time.deltaTime * 100);
         }
 
         if (Input.GetKey ("left")) {
 
             cursor.transform.Translate (Vector3.left * Time.deltaTime * 100);
         }
         if (Input.GetKey ("right")) {
 
             cursor.transform.Translate (Vector3.right * Time.deltaTime * 100);
         }
         
     }
 
 }


[1]: /storage/temp/94592-whatsapp-image-2017-05-23-at-114111-am.jpeg

In the attached screenshot you can see the detecting ray (the green one) starting from the cube and the resulting normal ray (the red one) that is used to find the new position.

whatsapp-image-2017-05-23-at-114111-am.jpeg (50.5 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity3D - RTS Game - Instantiate object on buttonclick and make it follow the cursors position 2 Answers

Does RaycastHit.textureCoord assume collider mesh and renderer mesh the same? 2 Answers

How to prevent mesh to not go through other meshes 1 Answer

Detecting how many times a raycast collides with a single concave meshcollider 2 Answers

Mesh collider as bullet hit detection performance 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