- Home /
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?
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!
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)?
I've updated my original answer based on your comment and added an example chunk of code that will hopefully help.
Your answer
Follow this Question
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