- Home /
Raycasting to avoid falling (FPS game)
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” (http://unifycommunity.com/wiki/index.php?title=FPSWalkerEnhanced) 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;
}
Answer by Loius · Jun 05, 2012 at 06:04 PM
Just set your ray start point to player.position + player.GetPlanarVelocity() * (an amount of time).
Or you can make it a constant buffer distance from 'pits' by using player.position + player.GetPlanarVelocity.normalized() * (a distance).
Looks like your 'moveDirection' would work as velocity -
function GetPlanarVelocity() : Vector3 { return new Vector3(moveDirection.x, 0, moveDirection.z); }
Shoot your ray straight down from that point. If it can travel farther than [1 meter] then it's a 'pit'.
If you can 'hover' above safe ground, you might need to raycast straight down from player to find the floor, then nudge over like above, then raycast straight down from the new point.
I was able to use your advice as a starting point (thanks for that). But now another problem has risen: when the player reaches the edge (and the game properly deter$$anonymous$$es there is a "pit"), he cannot move anymore. Not in any direction (unless I tick off the DownAllowFall).
if (DontAllowFall) {
RaycastHit groundCheck;
Vector3 moveWhere = new Vector3(moveDirection.x, 0, moveDirection.z);
if (!Physics.Raycast(myTransform.position + moveWhere * 1, Vector3.down, out groundCheck, 5)) {
print("NO GROUND");
moveDirection = new Vector3(0, 0, 0);
}
else {
print("GROUND");
moveDirection = new Vector3(inputX * input$$anonymous$$odifyFactor, -antiBumpFactor, inputY * input$$anonymous$$odifyFactor);
moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;
}
}
else {
moveDirection = new Vector3(inputX * input$$anonymous$$odifyFactor, -antiBumpFactor, inputY * input$$anonymous$$odifyFactor);
moveDirection = new Vector3(inputX, -antiBumpFactor, inputY);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;
}
What is moveDirection co$$anonymous$$g into this? You need to use mD after it's been translated to local space (TransformDirection) or it won't line up right.
You may also need to only do the check if moveWhere.magnitude > 0
You should be able to say "if no ground then stop player" and that's it.
Answer by GutoThomas · Jun 05, 2012 at 04:01 PM
Why don't you just add some colliders in an empty game object and position them in the places where the player won't be able to walk?
I guess is a pretty quick solution and will not require Raycast, which isn't the most light thing for the CPU.
Like I said, "Due to the sheer amount of level geometry, I cannot use colliders for this". It is just not feasible to add colliders everywhere. And that would also make it impossible for the player "jump up" to a place if there is a collider blocking.
But thanks for the input. It just not feasible solution for me.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Modifing an Enum value by a String? 2 Answers
Ray.direction editing not working in C#? 1 Answer
Where can i find an easy raycast shooting tutorial for C#? 2 Answers
shooting multiple enemies using raycast 2 Answers