Before updating to Windows 10 you may need to return your Pro license. More information here

Why does this script to detect fall distance not work?

I know it's really long, but can you please tell me why this script isn't working? My setup is a set of cubes, each 1x1x1, and if I fall more than 1 cube height,

 Debug.LogError("You died LOLOLOL");

should get called. This is based off the CharacterMotor.js script (I would have put a diff up, but I can't.)

     // We were not grounded but just landed on something
     else if (!grounded && IsGroundedTest()) {
         grounded = true;
         fallDistance = tr.position.y-fallStartPos.y;
         jumping.jumping = false;
         SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
     // We want to see how far they've fallen since starting to fall....
     else if (!grounded && !IsGroundedTest()) {
         fallDistance = tr.position.y-fallStartPos.y;
     if (fallDistance<-1.0) {
         Debug.LogError("You died LOLOLOL");
more ▼

asked Dec 29, 2011 at 11:57 PM

avatar image

41 13 9 12

Question edited to remove unnecessary code (about 95% - CharacterMotor is a reeeeally long script)

Dec 30, 2011 at 01:49 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This CharacterMove.js script is the worst nightmare of any programmer: it's highly complex, and almost every change you make produces nothing but headaches and collateral effects. You could try something simpler like the Move example with a few modifications: save the current y coordinate when the character is grounded, and compare it to the current y coordinate in Update - something like this:

var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0;

private var controller : CharacterController; private var moveDirection : Vector3 = Vector3.zero; private var floorHeight: float; // current height

function Start(){ controller = GetComponent(CharacterController); floorHeight = transform.position.y; }

function Update() { if (controller.isGrounded) { /// save the current floor height when grounded floorHeight = transform.position.y;

     moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection *= speed;
     if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;
 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);

 /// check if character fell more than 1 unit from last floor height:
 if (floorHeight - transform.position.y > 1){
     Debug.LogError("You died LOLOLOL");

} To use this script, attach it to the player - but remember to disable or remove that damned CharacterMotor.js script!

more ▼

answered Dec 30, 2011 at 12:31 AM

avatar image

61.6k 57 212 361

You are wonderful, and I love people like you. Thank you so much for relieving all the headache I had from this stupid script!

EDIT: how can I make the "capsule" of the camera not be a capsule anymore?

Dec 30, 2011 at 01:27 AM macweirdo

If you're talking about the First Person "body", it's just a child object named "Graphics". You can get your own model, child it to the First Person object and delete Graphics.
NOTE: The CharacterController itself is a capsule collider, thus no matter what's your model shape, its collider will always be a capsule.

Dec 30, 2011 at 03:22 AM aldonaletto

"its collider will always be a capsule" and there's no way I can change that?

Dec 30, 2011 at 01:22 PM macweirdo

No, the CharacterController is a specialized "creature" that comes with its own collider. You can change its dimensions, but not its shape, not even its orientation (it's always a vertical capsule). Adding other colliders to it only makes things worse: there's a bug in the CharacterController that disturbs the character movement when childed colliders are touching the capsule.

Dec 30, 2011 at 01:41 PM aldonaletto

Dangit. Thanks for the help, in any case. Now can you answer "http://answers.unity3d.com/questions/200167/how-do-i-animate-procedural-textures.html" this question?

Dec 30, 2011 at 01:50 PM macweirdo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 29, 2011 at 11:57 PM

Seen: 1384 times

Last Updated: Dec 30, 2011 at 01:50 PM