I’m trying to make it so that the player won’t be able to walk into a place where he would fall. Meaning I want to do a raycast into the direction in which the player is moving (which isn’t necessarily forward as the player might for example strafe) and if there is a fall of more than 1 unit (no need to be this precise), prevent movement to that direction.
Due to the sheer amount of level geometry, I cannot use colliders for this. Any help would be appreciated (preferably in C#).
I searched for a similar solution and I found this thread: “Raycast to check for potential pitfalls” (http://forum.unity3d.com/threads/138406-Raycast-to-check-for-potential-pitfalls), but I seem to be unable to modify it to suit my needs.
Acknowledgement: As a basis for this script I am using “FPSWalkerEnhanced” (unifycommunity.com) by Eric Haines (Eric5h5).
A side note: I copied (or at least tried to copy) only the relevant parts from the script to this post, since the script itself is exceedingly long (with a lot of actions which have nothing to do with player movement). So if it seems there is an obvious omission, that’s probably the reason.
void Update() {
// Get axis
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// Prevent sidestepping speed increase
float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f)? .7071f : 1.0f;
// Determine if player is moving
if(oldPos.x != transform.position.x || oldPos.z != transform.position.z) {
isMoving = true;
}
else {
isMoving = false;
}
oldPos = transform.position;
// If player is touching the ground
if (grounded) {
// If player was falling
if (falling) {
falling = false;
// Give damage if fallen for too much
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold) {
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
}
// Movement direction
moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;
// Jumping
if (Input.GetButtonDown("Jump")) {
Jump();
}
}
else {
// If player was not falling
if (!falling) {
falling = true;
// Determine start position of fall
fallStartLevel = myTransform.position.y;
}
// If airControl is allowed, allow movement while on air
if (airControl) {
moveDirection.x = inputX * speed * inputModifyFactor;
moveDirection.z = inputY * speed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the player
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
}