• 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 billyjoetyler · Oct 25, 2015 at 01:42 PM · raycastdistancevertices

distance between raycast hit and vertex

I am trying to find the distance between a raycast hit point and vertices on a mesh. When I click the mouse button my character sends out a raycast which hits an object, once this happens I put the vertices in the object into a list. Now I want to find the distance between each vertex and the hit.point of the raycast.

Code:

     using UnityEngine;
     using System.Collections;
     
     public class DistanceTest : MonoBehaviour {
     
         RaycastHit hit;
         // Use this for initialization
         void Start () {
     
         }
         
         // Update is called once per frame
         void Update () {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             if(Input.GetMouseButtonDown(0)){
                 if (Physics.Raycast(transform.position, fwd, out hit, 100.0F)) {
                     distance();
                 }
             }
         }
     
         void distance(){
             MeshFilter mf = hit.collider.GetComponent<MeshFilter>();
             Mesh mesh = mf.mesh;
             
             Vector3[] vertices = mesh.vertices;
             
             print(vertices.Length);
             
             int i = 0;
             while(i < vertices.Length){
                 print(Vector3.Distance(transform.InverseTransformPoint(vertices[i]), transform.InverseTransformPoint(hit.point)));
                 i++;
             }
         }
     }
     
Comment
Add comment · Show 2
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 DiegoSLTS · Oct 25, 2015 at 01:49 PM 0
Share

So? What's your question? You get an error message? Something doesn't work?

avatar image billyjoetyler · Oct 25, 2015 at 01:59 PM 0
Share

it reports that the distance between the hit point and the object is over 1000 meters while the actual distance is no more than 10 meters. it seems to think that the vertices are around the middle of the scene whereas they are not.

1 Reply

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

Answer by Nimred · Oct 25, 2015 at 02:07 PM

The vertices would be stored in local space, not world space. You should either use InverseTransformPoint on the hit point but not the vertex, to compare positions in local space; Or use TransformPoint on the vertex but not the hit point to compare positions in world space.

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 billyjoetyler · Oct 25, 2015 at 02:22 PM 0
Share

This partially works, but the distance is 3 to 5 meters away, when it should range from 0 to 10.

avatar image Nimred billyjoetyler · Oct 25, 2015 at 02:26 PM 0
Share

Ah, I notice that you are also using "transform" with InverseTransformPoint. However, you should be converting to the local space of the object you hit, not the local space of your script object. Therefore, use hit.collider.transform ins$$anonymous$$d of just transform.

If this still does not work, please post the updated code so we can see what's wrong.

avatar image billyjoetyler · Oct 25, 2015 at 03:24 PM 0
Share

thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Get the distance between the origin and HitPoint for Raycast? 1 Answer

Is Raycast relatived to screen resolution? 1 Answer

Math - calculate position in world space from ray on infinite plane 2 Answers

Find height above a mesh without raycasting (for bobbing a boat). 1 Answer

Picking up rigidbody objects 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