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 MattSawrey · Feb 10, 2014 at 10:57 PM · updatedistancelinecast

Linecast not returning distance

I've got the following script that draws a line between two objects, one moveable (posChar) and one static (posFloor). I'm trying to use it to calculate the distance between these two objects, but the value of the float "distance" from the following code is always returning zero, regardless of where I move my character. Any advice on what I'm doing wrong would be appreciated. Thanks.

     public GameObject characterPos;
     public GameObject floorPos;
 
     Vector3 posChar;
     Vector3 posFloor;
 
     public float distance;
 
 
     void Update()
     {
         posChar = characterPos.transform.position;
         posFloor = floorPos.transform.position;
 
         Debug.DrawLine(posChar, posFloor);
 
         RaycastHit hit;
         
         if(Physics.Linecast(posChar, posFloor, out hit, LayerMask.NameToLayer("Ground")))
         {
             distance = hit.distance;
         }
         Debug.Log(distance);
     }
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

2 Replies

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

Answer by kat0r · Feb 10, 2014 at 11:05 PM

You need to use

 if(Physics.Linecast(posChar, posFloor, out hit, 1 << LayerMask.NameToLayer("Ground")))

instead of

 if(Physics.Linecast(posChar, posFloor, out hit, LayerMask.NameToLayer("Ground")))

If you just want the distance, why not use

 Vector3.Distance(a,b)

?

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 MattSawrey · Feb 10, 2014 at 11:42 PM 0
Share

Thanks.

This worked, but the value of "distance" is now slowly decreasing by roughly 0.004f every frame whilst my player object and the floor object are both stationary. I found that this also happened using the Vector3.Distance(a, b) method, which I was originally using (as you've suggested). It's the reason behind my trying this linecast method.

Do you have any idea why the value of "Distance" would decrease every frame by this amount (0.004f) whilst the two objects are stationary? The value seems to reset itself when I move the player, but for my purposes I need a precise and stable measurement between the player and the floor at all times.

avatar image kat0r · Feb 11, 2014 at 09:42 AM 0
Share

If both objects really are stationary, this cannot happen (there may be some "flickering" because floats are not exact, but approximations). If distance is decreasing, so would the distance your linecast gets, btw.
I would first check WHY those objects are see$$anonymous$$gly getting closer, without seeing your code I would assume it is because of physics. (like gravity)

avatar image MattSawrey · Feb 11, 2014 at 08:10 PM 0
Share

You were right. It was to do with the fact that "Use Gravity" was ticked on my character's Rigidbody. I unchecked that and the problem stopped. Strange. Thanks for the help!

avatar image
0

Answer by karlhulme · Feb 10, 2014 at 11:10 PM

Hi,

Physics.LineCast will tell you if a line drawn between the two points hits a collider, and if it does, the distance to that collider. I'm guessing one of two things is happening…

  • The Physics.LineCast is returning false, so distance is never being set, hence Debug.Log shows zero.

  • The Physics.LineCast is returning true because you've got a collider on the ground, which is intersecting with posFloor - hence the distance is genuinely zero.

If you just want the distance between two points, consider Vector3.Distance.

Hope this helps.

Comment
Add comment · 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

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 increment the number by 1's to get distance traveled? 0 Answers

iOS Lag From Updating Score 0 Answers

calculating vertical distance 1 Answer

My look at script not working? 0 Answers

is it possible change picture or video content once published? 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