Search Unity

Two diffrent forces on one object

Discussion in 'Physics' started by SiggesKod, Jan 23, 2020.

  1. SiggesKod

    SiggesKod

    Joined:
    Jan 23, 2020
    Posts:
    6
    Is it possible to give the left front wheel one force and the right front wheel another?

    If, please guide me to the correct information.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Yes, simply call Rigidbody.AddForceAtPosition twice, passing the world position of the points to apply the force at.
     
    SiggesKod likes this.
  3. SiggesKod

    SiggesKod

    Joined:
    Jan 23, 2020
    Posts:
    6
    Do you have any example code?
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    You may try adding something like this to a script that is in the same GameObject as the Rigidody. Specify the references to the wheel components, then the relative forces you want to add at each of them. For example, a force of [0, 0, 10] means 10 newtons applied in the forward direction.

    Code (CSharp):
    1. public Transform leftWheel;
    2. public Transform rightWheel;
    3. public Vector3 leftForce;
    4. public Vector3 rightForce;
    5.  
    6. void FixedUpdate ()
    7. {
    8.     GetComponent<Rigidbody>.AddForceAtPosition(leftWheel.position, transform.TransformDirection(leftForce));
    9.     GetComponent<Rigidbody>.AddForceAtPosition(rightWheel.position, transform.TransformDirection(rightForce));
    10. }
    11.