Search Unity

Bug rigidbody It's going up and bounces when is moving

Discussion in 'Scripting' started by osamaamir, Oct 29, 2021.

  1. osamaamir

    osamaamir

    Joined:
    Apr 25, 2017
    Posts:
    10
    hello
    I have a problem with rigidbody, This is a simple character controller script. the problem is i have created some square floor tiles and each floor has mesh collider. soo when the character moving fast on thes floor tiles the charachter will going up a little bit !! this is a image and a code of script




    Code (CSharp):
    1. public class character_controller : MonoBehaviour {
    2.     [Header("Speeds")]
    3.      public float move_speed;
    4.      public float move_smooth;
    5.      public float rotate_speed;
    6.  
    7.  
    8.      [Header("Physics")]
    9.      public float gravity_force = -10.0f;
    10.  
    11.  
    12.      [Header("Distances")]
    13.      public float ground_min_dis = 0.25f;
    14.      public float ground_max_dis = 100;
    15.  
    16.  
    17.      [Header("Layers")]
    18.      public LayerMask ground_layer;
    19.  
    20.  
    21.      [HideInInspector] public Rigidbody rb;
    22.      [HideInInspector] public Vector3 dir;
    23.      [HideInInspector] public Vector3 input;
    24.      [HideInInspector] public Vector3 input_smooth;
    25.      [HideInInspector] public bool move_to_direction;
    26.      [HideInInspector] public bool rotate_to_direction;
    27.      [HideInInspector] public bool can_move;
    28.      [HideInInspector] public bool is_on_ground;
    29.  
    30.  
    31.      private float ground_distance;
    32.      private RaycastHit ground_hit;
    33.    
    34.  
    35.      // method: start is called before the first frame update
    36.      void Start() {
    37.          rb = GetComponent<Rigidbody>();
    38.          can_move = true;
    39.          move_to_direction = true;
    40.      }
    41.  
    42.  
    43.      // method: update is called once per frame
    44.      void Update() {
    45.          move_input();
    46.          check_ground_distance();
    47.      }
    48.  
    49.  
    50.      // method: fixed update is called every fixed framerate frame
    51.      void FixedUpdate() {
    52.          if(
    53.              move_to_direction
    54.          ) {
    55.              move();
    56.          }
    57.      }
    58.  
    59.  
    60.      // method: move input
    61.      private void move_input() {
    62.          input.x = Input.GetAxis("Vertical");
    63.          input.z = Input.GetAxis("Horizontal");
    64.      }
    65.  
    66.  
    67.      // method: move character
    68.      private void move() {
    69.          // input smooth for moving character smoothly
    70.          input_smooth = Vector3.Lerp(input_smooth, input, move_smooth * Time.deltaTime);
    71.          // get the forward facing direction of the character
    72.          var forward = Vector3.forward;
    73.          // get the left facing direction of the character
    74.          var right = Vector3.left;
    75.        
    76.          // determine the direction
    77.          dir = (input_smooth.x * forward) + (input_smooth.z * right);
    78.          // normalize direction
    79.          if (
    80.              dir.magnitude > 1f
    81.          ) {
    82.              dir.Normalize();
    83.          }
    84.          Vector3 target_position = rb.position + dir * move_speed * Time.deltaTime;
    85.          Vector3 target_velocity = (target_position - transform.position) / Time.deltaTime;
    86.          target_velocity.y = rb.velocity.y;
    87.          if(
    88.              can_move
    89.          ) {
    90.              rb.velocity = target_velocity;
    91.          }
    92.          if(
    93.              input.magnitude > 0.01f
    94.          ) {
    95.              // start rotate to direction
    96.              if(
    97.                  rotate_to_direction == true
    98.              ) {
    99.                  start_rotate_to_direction(dir, rotate_speed);
    100.              }
    101.          }
    102.      }
    103.  
    104.  
    105.      // method: rotate character to direction
    106.      public void start_rotate_to_direction(
    107.          Vector3 dir,
    108.          float speed
    109.      ) {
    110.          Vector3 forward;
    111.          Quaternion new_rotation;
    112.          forward = Vector3.RotateTowards(transform.forward, dir.normalized, speed * Time.deltaTime, 0.1f);
    113.          forward.y = 0;
    114.          new_rotation = Quaternion.LookRotation(forward);
    115.          transform.rotation = new_rotation;
    116.      }
    117.  
    118.  
    119.      // method: check ground distance
    120.      private void check_ground_distance() {
    121.          float distance;
    122.          Ray ray = new Ray(transform.position, Vector3.down);
    123.          // if the ray hit to the ground layer
    124.          if (
    125.              Physics.Raycast(ray, out ground_hit, ground_max_dis, ground_layer)
    126.          ) {
    127.              if(
    128.                  !ground_hit.collider.isTrigger
    129.              ) {
    130.                  distance = transform.position.y - ground_hit.point.y;
    131.                  ground_distance = (float)System.Math.Round(distance, 2);
    132.              }
    133.          }
    134.          // check if character on the ground
    135.          if(
    136.              ground_distance <= ground_min_dis
    137.          ) {
    138.              is_on_ground = true;
    139.          } else {
    140.              is_on_ground = false;
    141.          }
    142.          // apply force gravity when falling
    143.          if(
    144.              is_on_ground == false
    145.          ) {
    146.              rb.AddForce(transform.up * gravity_force * Time.deltaTime, ForceMode.VelocityChange);
    147.          }
    148.      }
    149. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
  3. osamaamir

    osamaamir

    Joined:
    Apr 25, 2017
    Posts:
    10
    PraetorBlue likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I'm gonna heap huge thanks on Praetor too for this... I had a pending issue with one of my games and this helps it a lot. THANKS PRAETOR!!!
     
    PraetorBlue likes this.