• 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 RoomofR · May 18, 2015 at 10:42 AM · javascriptraycastvariableraycasthit

Raycast Distance Help

 function Update () { 
 var fwd = transform.TransformDirection(Vector3.forward); 
 var $$anonymous$$t : RaycastHit; 
 }

Here I have a raycast on object that will try to detect objects in front of it. If there is an object then take the distance ($$anonymous$$t.distance) and change it into a int variable. (In Javascript)

So basically:

 var dis : int = $$anonymous$$t.distance
 

However for some reason it represents dis as 0.

Comment
Add comment · Show 3
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 alok-kr-029 · May 18, 2015 at 10:44 AM 0
Share

hope you need to take it as float ......

avatar image cobertos · May 18, 2015 at 11:03 AM 0
Share

Yes, you need to take it to a float. My guess is that the raycast is colliding with the same object that's firing it and then when you cast it to an integer, it chops off any small value that that original raycast might have returned. So do this:

1) Add a layer mask to your raycast to ignore the object that's shooting it

2) Store the distance in a float, not an integer

avatar image RoomofR · May 18, 2015 at 05:38 PM 0
Share

even if I take it as a float like :

 var dis : float = hit.distance;

the variable dis still equals to 0 when hit.distance equals to 8.2

1 Reply

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

Answer by redeemer · May 18, 2015 at 11:09 AM

Acording to your code, you're not actually raycasting anyt$$anonymous$$ng, but only declaring the variables to do so.

Try t$$anonymous$$s :

     function Update () {

             var rayLength = 10;
             var $$anonymous$$t : RaycastHit;
             var fwd = transform.TransformDirection (Vector3.forward);
         if (Physics.Raycast (transform.position, fwd, $$anonymous$$t, rayLength)) {
                     print ("Hit Distance : " + $$anonymous$$t.distance);
         }
     }


You should read t$$anonymous$$s too http://docs.unity3d.com/ScriptReference/Physics.Raycast.html.

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 RoomofR · May 18, 2015 at 05:43 PM 0
Share

I have this: function Update () { var fwd = transform.TransformDirection(Vector3.forward); var hit : RaycastHit; var dis : float = hit.distance;

     if (Physics.Raycast(transform.position, fwd, hit)){//IF I HIT SOMETHING
         print (dis); //Equals to 0 all the time
         print (hit.distance); //If equals to 8
     }
 }
avatar image redeemer · May 18, 2015 at 06:06 PM 1
Share

You are trying to assign the hit.distance before it's populated, what surprises me is the fact that you get 0 instead of null. Anyway, your code should be something like the above I already posted.

You also need to multiply your forward vector times the distance you want to cast. The forward, up or right vector are unitary, they only indicate the "direction" and always have a value of one. SO if you don't multiply it, you will be casting only 1 meter from you original pos.

Try the following, it should work :

     function Update () { 
     
     var fwd = transform.TransformDirection(Vector3.forward); 
     var hit : RaycastHit; 
     var rayLength = 10;
     
          if (Physics.Raycast(transform.position, fwd * rayLength, hit)){//IF I HIT SOMETHING
              var dis : float = hit.distance;
              print (dis); //Equals to 0 all the time
              print (hit.distance); //If equals to 8
          }
      }
 

I know it's kinda obvious, but just in case, be sure the object you want to detect have a collider and it's not set to trigger.

avatar image RoomofR · May 19, 2015 at 05:28 PM 0
Share

Thanks it works :D

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

"NullReferenceException: Object reference not set to an instance of an object" When Raycast hits and tries select a designated object 2 Answers

When I click, my enemy doesnt lose health... 0 Answers

Detect Object that are hit by a ray 3 Answers

Clicking on an Object to Make it the Variable Target 1 Answer

Is RaycastHit an expensive operation vs something via JS? 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