0
\$\begingroup\$

I encountered a problem in Unity related to the Character Controller, namely with checking whether the player is on the ground or not (isGrounded).

A player walking down a slope with a minimum angle of deflection is considered to be in the air, while the physics of the character attracts him to the ground.

I would like to detect that a character descending a slope is still touching the ground.

Is it possible to solve this problem using standard tools to update the surface under the player, or will I have to make a system of checking this myself using SphereCast or something similar?

private void HandleMovementAndJump() { IsGrounded = characterController.isGrounded; MoveVector = !inputState.MoveIsLocked ? InputManager.MoveInput : Vector2.zero; IsSprinting = !inputState.MoveIsLocked && staminaSystem.currentStamina > 0 &&[![enter image description here][1]][1] InputManager.SprintInput != 0 && MoveVector.y > 0f && CurrentState != FSMState.Crouch; CurrentInputVector = Vector2.SmoothDamp(CurrentInputVector, MoveVector, ref smoothInputVelocity, smoothInputSpeed); CurrentInputVectorInt = new Vector2(Mathf.Round(CurrentInputVector.x), Mathf.Round(CurrentInputVector.y)); if (CurrentInputVectorInt != Vector2.zero) { CurrentState = IsSprinting ? FSMState.Run : FSMState.Move; } else { CurrentState = FSMState.Idle; } Vector3 rightMove = transform.right * CurrentInputVector.x; Vector3 forwardMove = transform.forward * CurrentInputVector.y; float speed = GetCurrentSpeedValue(); CurrentMovementSpeed = Mathf.Lerp(CurrentMovementSpeed, speed, Time.deltaTime * smoothChangeSpeedValue); Vector3 movementVector = Vector3.ClampMagnitude(rightMove + forwardMove, 1f) * CurrentMovementSpeed; if (IsGrounded) { moveDirection = movementVector; } else { Vector3 airControl = movementVector * airControlFactor; moveDirection = new Vector3( moveDirection.x + airControl.x * Time.deltaTime, moveDirection.y, moveDirection.z + airControl.z * Time.deltaTime); } playerVelocity.y = IsGrounded && playerVelocity.y < 0 ? 0f : playerVelocity.y; if (!inputState.MoveIsLocked) { if (staminaSystem.currentStamina >= 25 && InputManager.JumpInput && IsGrounded && !IsCrouching) { CurrentState = FSMState.Jump; playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * playerGravity); //staminaSystem.LostStamina(10f); } } if (!IsGrounded) { CurrentState = FSMState.Fall; } playerVelocity.y += playerGravity * Time.deltaTime; if (characterController.collisionFlags == CollisionFlags.Above && playerVelocity.y > 0) { playerVelocity.y = 0f; } UpdateSlopeSliding(); characterController.Move((moveDirection + playerVelocity) * Time.deltaTime); } 

Character Controller

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Simply searching for an answer to you problem gave me link that seems to have the answer you need.

Basically it has to do with the movement vector you are applying to move your character down the slope is horizontal, and not pointing parallel to the plane of the slope. This will cause the player to move out into the air, and then fall down again onto the slope.

You need to adjust your movement vector based on the surface your character is standing on.

See this link for more details: https://discussions.unity.com/t/player-bouncing-down-slopes/776543/8

\$\endgroup\$
1
  • \$\begingroup\$ I needed to change the minimum value to a negative number and it worked playerVelocity.y = IsGrounded && playerVelocity.y < -2f ? -2f : playerVelocity.y; \$\endgroup\$ Commented Dec 9, 2024 at 8:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.