Search Unity

Rigidbody bouncing

Discussion in 'Physics' started by fernandopal, May 3, 2020.

  1. fernandopal

    fernandopal

    Joined:
    Jun 27, 2017
    Posts:
    2
    Hello everyone, I have a scene with a player (capsule collider + rigidbody + camera + my movement script) and a cube (cube collider + rigidbody).


    Every time that the player walks and join a collision with the cube it bounces back if the cube doesn't have space to move in the direction that is being pushed

    Here is a video showing the bouncing
    (when i go back im not pressing any button, its just the bounce)


    I don't want this to happen, i just want that if the player is moving the cube and it has no space it just stops moving, that bouncing is annoying and i dont know how to remove it. Can someone help me?

    Tganks in advance guys.
     
  2. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello,
    Can you post your movement script please (use the "insert code" function) ?
     
  3. fernandopal

    fernandopal

    Joined:
    Jun 27, 2017
    Posts:
    2
    Variables:
    Code (CSharp):
    1. public InputAction walkInput; https://prnt.sc/sbvxji
    2. public InputAction lookInput; https://prnt.sc/sbw16s
    3. public GameObject mainCamera; https://prnt.sc/sbvy26
    4. public float minYAngle = -50.0f;
    5. public float maxYAngle = 50.0f;
    6. public float sensitivityX = 6.0f;
    7. public float sensitivityY = 3.0f;
    8.  
    on Update:
    Code (CSharp):
    1. private void Update() {
    2.         var lookValue = lookInput.ReadValue<Vector2>();
    3.         currentX += lookValue.x * sensitivityX;
    4.         currentY -= lookValue.y * sensitivityY;
    5.  
    6.         currentY = Mathf.Clamp(currentY, minYAngle, maxYAngle);
    7.     }
    Move function (called on FixedUpdate):
    Code (CSharp):
    1. void Move() {
    2.         var moveInput = walkInput.ReadValue<Vector2>();
    3.  
    4.         Vector3 forward = mainCamera.transform.forward;
    5.         forward.y = 0;
    6.         forward.Normalize();
    7.  
    8.         Vector3 right = mainCamera.transform.right;
    9.         right.y = 0;
    10.         right.Normalize();
    11.  
    12.         Vector3 moveDir = right * moveInput.x + forward * moveInput.y; moveDir.Normalize();
    13.  
    14.         this.transform.position += moveDir * moveSpeed * Time.deltaTime;
    15.         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(forward), 0.2f);
    16.  
    17.         mainCamera.transform.rotation = Quaternion.Euler(currentY, currentX, 0);
    18.     }
    I think im not missing anything (just pasted the important things related to movement because that script contains a lot of things related to grabbing objects)
     
  4. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    I don't know if that'll solve your problem but I think you should change the rigidbody's transform instead of the gameobject's transform. The reason is that when you move the rigidbody, the transform is calculated after the next physics simulation step.

    https://docs.unity3d.com/ScriptReference/Rigidbody-position.html

    Let us know if that helped !