Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CharacterController boucing on ramp

Discussion in 'Physics' started by ksewo, Sep 14, 2019.

  1. ksewo

    ksewo

    Joined:
    Sep 14, 2019
    Posts:
    2
    Hello.
    How can I fix bouncing on ramp, so character sticks to ground?


    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.     [SerializeField] private string horizontalInputName;
    8.     [SerializeField] private string verticalInputName;
    9.     [SerializeField] private float movementSpeed;
    10.  
    11.     private CharacterController charController;
    12.  
    13.     [SerializeField] private AnimationCurve jumpFallOff;
    14.     [SerializeField] private float jumpMultiplier;
    15.     [SerializeField] private KeyCode jumpKey;
    16.     private Boolean isJumping;
    17.     public GameObject platform;
    18.  
    19.     private void Awake() {
    20.         charController = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     private void Update() {
    24.         PlayerMovement();  
    25.     }
    26.  
    27.     private void PlayerMovement() {
    28.         float vertInput = Input.GetAxis(verticalInputName);
    29.         float horizInput = Input.GetAxis(horizontalInputName);
    30.  
    31.         Vector3 forwardMovement = transform.forward * vertInput;
    32.         Vector3 rightMovement = transform.right * horizInput;
    33.         Vector3 moveDir = Vector3.zero;
    34.  
    35.         RaycastHit hit;
    36.         if (!isJumping && Physics.Raycast(transform.position, Vector3.down, out hit)) {
    37.             moveDir = SlopeAngle(forwardMovement + rightMovement, hit.normal);
    38.             Debug.DrawLine(hit.point, moveDir, Color.red, 3f);
    39.         }
    40.  
    41.         moveDir.Normalize();
    42.  
    43.         charController.SimpleMove(moveDir * movementSpeed);
    44.  
    45.         JumpInput();
    46.     }
    47.  
    48.     private Vector3 SlopeAngle(Vector3 vector, Vector3 normal) {
    49.         return Vector3.ProjectOnPlane(vector, normal);
    50.     }
    51.  
    52.     private void JumpInput() {
    53.         if(Input.GetKeyDown(jumpKey) && !isJumping && charController.isGrounded) {
    54.             isJumping = true;
    55.             StartCoroutine(JumpEvent());
    56.         }
    57.     }
    58.     private IEnumerator JumpEvent() {
    59.         float timeInAir = 0;
    60.         do {
    61.             float jumpForce = jumpFallOff.Evaluate(timeInAir);
    62.             charController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);
    63.             timeInAir += Time.deltaTime;
    64.             yield return null;
    65.         } while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
    66.  
    67.         isJumping = false;
    68.     }
    69. }
    70.  
     
  2. ksewo

    ksewo

    Joined:
    Sep 14, 2019
    Posts:
    2
    Can I do something with ground's normal for it? I tried rotating character by degrees of ground but it did not worked.