Hello everyone, I’m trying to add ladders to my first person shooter, where the character can climb up when staying in front of the ladder object with the tag “ladder” and holding down “w” (moving forward). However, my character keeps falling down even tough velocity.y and gravity is set to 0 as you can see in my script:
void Update()
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
//Ladder
if(Physics.Raycast(transform.position + Vector3.up * .8f, transform.TransformDirection(Vector3.forward), out RaycastHit raycastHit, 1.2f)){
if(raycastHit.transform.tag == "ladder"){
ladder = true;
move.x = 0;
move.y = move.z;
move.z = 0;
velocity.y = 0;
gravity = 0;
isGrounded = true;
controller.Move(move * speed * Time.deltaTime);
}
else{
ladder = false;
}
}
else{
ladder = false;
}
if(!leiter){
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
gravity = -20f;
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
controller.Move(move * speed * Time.deltaTime);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
Someone an idea what might be wrong?