• 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 09, 2014 at 03:56 PM · gameobjectdistanceheight

Calculate distance between player and floor

I'm making a game in which the player is free falling. I want the player to know how much time they have left until they hit the floor. Currently I'm calculating the distance between the player and the floor as in the script below. This doesn't give me the actual distance between the player and the floor though as the calculation for "floorDist" continues even whilst the player is on a static ledge above the ground, or stood on the ground. Any advice on how to better calculate this distance would be appreciated.

     public GameObject floor;
     public float floorDist;
     
     void Awake () 
     {
         floorDist = gameObject.transform.position.y;
 
     }
     
     void Update()
     {
         FloorMeasure();
         floorDist = gameObject.transform.position.y;
     }
     
     void FloorMeasure()
     {
         floorDist = gameObject.transform.position.y - floor.transform.position.y;
     }
 
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 ankush_Kushwaha · Feb 09, 2014 at 04:09 PM 0
Share

What Floor$$anonymous$$easure(); is returning (it should return correct value ) ? and why are you using

floorDist = gameObject.transform.position.y; in your Update().

avatar image ankush_Kushwaha · Feb 09, 2014 at 04:10 PM 0
Share

I think you dont need to use floorDist = gameObject.transform.position.y; in your Update(). and everything will be ok

2 Replies

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

Answer by Linus · Feb 09, 2014 at 04:00 PM

You can use linecast https://docs.unity3d.com/Documentation/ScriptReference/Physics.Linecast.html

Then access the distance of the hit https://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

Look up on what code is used to see if a character can jump. you can use much of the same code.

http://answers.unity3d.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html

Comment
Add comment · Show 1 · 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 09, 2014 at 08:07 PM 0
Share

Thanks. I just started learning C sharp last week so I appreciate the help. I'm trying to use a linecast to do it now. Here's what I've got:

     public float floorDist;
 
     public GameObject characterPos;
     public GameObject floorPos;
 
     Vector3 posChar;
     Vector3 posFloor;
 
 
     void Awake () 
     {
         posChar = characterPos.transform.position;
         posFloor = floorPos.transform.position;
     }
     
     void Update()
     {
         Floor$$anonymous$$easure();
         Debug.Log("Distance to Floor = "+floorDist);
     }
     
     void Floor$$anonymous$$easure()
     {
 
         RaycastHit hit;
 
         if (Physics.Linecast(posChar, posFloor, out hit))
         {
             floorDist = hit.distance;
         }
     }

It doesn't seem to be returning the distance between the player and the ground. I'm not sure why. I've checked the gameobjects I've assigned to "characterPos" and "floorPos" and they're correct. Any ideas?

avatar image
0

Answer by Vardan Meliksetyan · Feb 09, 2014 at 04:01 PM

Hi, you must use Raycast, it is simple and usefull !

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.

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

21 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

Related Questions

How to make an gameobject dissapear when player gets close to it 0 Answers

Help with distance between multiple objects 3 Answers

Edit SmoothFollow.js to change height and distance vars smoothly on some triggers 1 Answer

Destroying the object that I am selecting 1 Answer

Difference between scale and height in a gameobject with charactercontroller 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