Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Adding force to a riggidbody with the same values everytime produces different results

Discussion in 'Physics' started by palchoso, Apr 11, 2020.

  1. palchoso

    palchoso

    Joined:
    Jun 27, 2019
    Posts:
    5
    Hello,

    I was making a Script that let me throw object at the direction that the camera is pointing.
    Everytime I throw the object, it seems that the force applied to the object is random. Im using this:

    Code (CSharp):
    1. grabbed.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * shootForce);
    Where:

    - transform.forward is the direction the camera is facing
    - shootForce is the force of the throw

    Thank you for your help
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,434
    This should resolve the issue:
    • Remove Time.deltaTime from the calculation. A force is a time-independent magnitude. It is integrated along time internally by the physics engine.
    • Ensure to make the AddForce call from FixedUpdate, not from Update.
     
    NaTurek and palchoso like this.
  3. palchoso

    palchoso

    Joined:
    Jun 27, 2019
    Posts:
    5

    ahhh, you're right, i didn't remember that things. Thank you so much
     
    Edy likes this.