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 macweirdo · Dec 22, 2011 at 10:09 PM · physicspositionfalling

How do I detect how far a character has fallen?

If I have a MainCamera object with CharacterMotor.js and other related scripts attached, how do I detect how far said MainCamera has fallen?

Let's say it fell more than one unit (or whatever the Unity measurement system is called.) How do I detect that?

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

1 Reply

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

Answer by Aleron · Dec 22, 2011 at 10:26 PM

The easiest way, without seeing more context or code from you, is to set it's starting position in a class variable when the object is created (put the storing of the variable in the Awake() or Start() of a component) then when it has stopped falling use Vector3.Distance() to find the distance between it's starting position and it's final position. If you just want the vertical distance traveled you can simply subtract the startingPosition.y and the finalPosition.y.

EDIT based on the additional information below:

At about line 265 (inside the UpdateFunction function) there should be a line like the following:

 if (grounded && !IsGroundedTest()) {

Inside that if block is where you would store their starting position/height. Then if you only want to check how far they've fallen once they hit the ground, you would go down to the 'else if' below it that looks like the following:

 else if (!grounded && IsGroundedTest()) {

And get it's current grounded position/height, then you can calculate the distance between them.

If you want to check how far they've fallen every update until they hit the ground, then you'll need to add your own else if to update things as they fall.

Overall an quick and dirty example of that section would be, you will need to add a global Vector3 variable named fallStartPos and a global float variable named fallDistance:

     if (grounded && !IsGroundedTest()) {
     grounded = false;
 // ******************************* START NEW CODE *******************************
             fallStartPos = tr.position;
 // ******************************** END NEW CODE ********************************
     
     // Apply inertia from platform
     if (movingPlatform.enabled &&
         (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
         movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
     ) {
         movement.frameVelocity = movingPlatform.platformVelocity;
         movement.velocity += movingPlatform.platformVelocity;
     }
     
     SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
     // We pushed the character down to ensure it would stay on the ground if there was any.
     // But there wasn't so now we cancel the downwards offset to make the fall smoother.
     tr.position += pushDownOffset * Vector3.up;
 }
 // We were not grounded but just landed on something
 else if (!grounded && IsGroundedTest()) {
     grounded = true;

 // ******************************* START NEW CODE *******************************
             fallDistance = Vector3.Distance(tr.position, fallStartPos);
 // ******************************** END NEW CODE ********************************
 
     jumping.jumping = false;
     SubtractNewPlatformVelocity();
     
     SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
 }
 // ******************************* START NEW CODE *******************************
     // We want to see how far they've fallen since starting to fall....
     else if (!grounded && !IsGroundedTest()) {
         fallDistance = Vector3.Distance(tr.position, fallStartPos);
     }
 // ******************************** END NEW CODE ********************************


I've never really looked at or worked with this particular script before, but from skimming it and based on what you said, this should give you a step in the right direction. I hope it helps!

Comment
Add comment · Show 4 · 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 macweirdo · Dec 22, 2011 at 11:51 PM 0
Share

Well, my code is just the Character$$anonymous$$otor.js that comes with Unity as an example. So what would I do to it to find how far it has just fallen (since it has become ungrounded)?

avatar image Aleron · Dec 23, 2011 at 12:17 AM 0
Share

I've updated my original answer based on your comment and added an example chunk of code that will hopefully help.

avatar image macweirdo · Dec 23, 2011 at 03:35 AM 0
Share

Thank you! :D

avatar image Aleron · Dec 23, 2011 at 04:38 AM 0
Share

You're welcome. I'm glad it helped. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Cubes vibrating and sliding while ontop of eachother 0 Answers

2D 360 degress platformer example needed 0 Answers

Move object to specific position with physics 2 Answers

Falling through ground with colliders 10 Answers

How To Freeze An Object Motion? 3 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