How do i ajust the player height acording to the height of the roof in a first person game?
Hey. I'm trying to make a first person game with the player needing to crouch under objects but I want the players standing height to be depending roof.
if (Input.GetButton("Crouch"))
{
player.height -= 1;
IsCrouched = true;
if (player.isGrounded == true)
{
Speed = CrouchSpeed;
JumpForce = CrouchJumpForce;
}
}
else
{
player.height += 1;
IsCrouched = false;
Speed = WalkSpeed;
}
player.height = Mathf.Clamp(player.height, 4, 8);
this is the code I'm using so far however the player instead clips through the ground slightly preventing me from moving.
let me explain what I'm trying to achieve a little better,
When the player pressed crouch, the character will decrease height until it reaches its minimum height of 4, when the player releases crouch, the character will increase height until it reaches one of two outcomes,
1: the player reaches it's highest height value of 8
2: the roof is preventing to character from getting any taller
I hope this explains what I want to achieve and if anyone knows how to fix it, please help me
T-T
Answer by tormentoarmagedoom · Jun 20, 2018 at 01:50 PM
Good day.
Then you only need to know the height of the roof and compare the player.height with the roof height
If need the roof heigh is not constant, then you should use colliders. All rofs must havea collider, and create a collider in the player or create a empty object with a collider, child of the player.
Then this empty object must move at same time as player.height. When the empty object detects a collision with some object tagged or named "roof" make it says to your player main screipt that is touching a roof.
OR something like this.
Bye!