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;
SubtractNewPlatformVelocity();
SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
}
// We want to see how far they've fallen since starting to fall....
// THIS IS THAT PART
else if (!grounded && !IsGroundedTest()) {
fallDistance = tr.position.y-fallStartPos.y;
}
if (fallDistance<-1.0) {
Debug.LogError("You died LOLOLOL");
}
...
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,
Input.GetAxis("Vertical"));
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!