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 Redtwoactual · Oct 04, 2013 at 05:21 AM · raycastdistance

Subtracting rays

I am trying to subtract two ray values from each other in order to determine the change in distance factor. Ideally i would like to subtract my ray distance from last update cycle, from my ray distance on this update cycle, thus obtaining a number. This will be used to determine the rate of climb for my aircraft. this is the code I have come up with: var timerdistance:float;

 timerdistance += Time.deltaTime;
     var groundHit : RaycastHit;    
     Physics.Raycast( rigidbody.transform.position - Vector3.up, -Vector3.up, groundHit );
     if (timerdistance > 1){
     var groundHit1 : RaycastHit;
     Physics.Raycast( rigidbody.transform.position - Vector3.up, -Vector3.up, groundHit1 );
     timerdistance = 0;
     }
     groundDisplay.text = Mathf.Round( groundHit.distance - groundHit1.distance ) + "m";

I am not getting the right result. please help. Thanks.

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 robertbu · Oct 04, 2013 at 05:35 AM

If I understand what is going on here correctly, you should be getting zero. That is, your first Raycast() happens every frame. Your second only happens once a second, but it happens is the same frame as your first, so it will be zero the first frame, and then it will change as groundHit1 becomes stale, then it will zero again the next time groundHit1 is calculated.

Plus, if your terrain is not flat and if you ironed out the issues with this code, the rate of climb would still be wrong. For example, if the plane is flying level and the ground rose up, then the indicator would indicate a descending plane. It seems to be you just want to report the change in 'Y' position over time to get the rate of climb.

Whatever method you select, I suggest you use InvokeRepeating() as some frequency (0.25 seconds to 1.0 seconds depending on how frequently you want the display to update), and report the difference between the current reading and the previous reading.

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
avatar image
0

Answer by Redtwoactual · Oct 04, 2013 at 04:50 PM

I forgot about change in terrain! You are right this method will not work. I may have to go to a y velocity.magnitude model. Thanks, I was so busy over thinking I missed the obvious!

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

14 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

Related Questions

How to get the distance between two objects in feet/meter? 1 Answer

Is it possible to measure distance's between raycast intersections? 2 Answers

Plane.Raycast() out distance incorrect? 1 Answer

how do I restrict my shooting script's raycast to a certain range? 2 Answers

distance between raycast hit and vertex 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