Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Resolved Using AddRelativeForce on Rigidbody the ball is never continue rolling out why ?

Discussion in 'Physics' started by shamenraze1988, Jul 14, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    The ball is rolling a bit then keep rolling almost in the same place.
    If I'm using only AddForce then the ball will roll out a box on a slope but not in a straight static direction forward but a bit also to the left or right. That is why I'm using AddRelativeForce.

    In both cases, I'm using speed 500 but when using AddRelativeForce the ball is not rolling out seems like not even trying. Only rolling a bit then rolling from side to side almost in place.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Rolling : MonoBehaviour
    7. {
    8.     public NaviManager naviManager;
    9.     public float speed;
    10.     public string onCollisionState;
    11.  
    12.     private Rigidbody rb;
    13.     private bool isTouching = false;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void FixedUpdate()
    23.     {
    24.         if (naviManager.crateOpenOnce)
    25.         {
    26.             if (isTouching)
    27.             {
    28.                 //rb.AddForce(Vector3.right * speed * Time.deltaTime);
    29.                 rb.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
    30.             }
    31.             else
    32.             {
    33.                 rb.velocity = rb.velocity * 0.95f * Time.deltaTime;
    34.             }
    35.         }
    36.     }
    37.  
    38.     private void OnCollisionEnter(Collision collision)
    39.     {
    40.         if (collision.gameObject.name == "Crate_0_0")
    41.         {
    42.             onCollisionState = "Touching !!!";
    43.  
    44.             isTouching = true;
    45.         }
    46.     }
    47. }
    48.  
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,020
    That's because AddRelativeForce is just that: Adds a force in a direction relative to the object. A relative force forwards is applied in the forward direction of the ball. But as the ball rolls forward, it's forward direction also rolls so it ends pointing down. Then the relative force forward is just pushing the ball in the down direction.

    To verify this, select your ball in the Scene view while playing the scene. In the toolbar, ensure the Transform mode is selected (key W) and the reference mode shows Local (key X until the button shows "Local"). Then you'll see the local transform of your ball. The forward direction is the Z axis (blue). That's the actual direction of the force you're applying.
     
    shamenraze1988 likes this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,020
    BTW, you should remove Time.deltaTime from your calculations, both force and velocity. Those are time-independent magnitudes used in this context. Otherwise, changing the physics rate (Fixed Timestep) will have dramatic effects.
     
    shamenraze1988 likes this.
unityunity