Search Unity

Problem with car physics

Discussion in 'Scripting' started by DarkSoul, May 11, 2010.

  1. DarkSoul

    DarkSoul

    Joined:
    Dec 24, 2009
    Posts:
    37
    Hi.
    I created a car with physics, but my car is moving to the left, I don't press any key, the problem is the centerOfMass, if I delete the centerOfMass, the movement of the car is correct (but without central mass). The code is:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LogicCar : MonoBehaviour {
    6.     //Variables
    7.     private float Velocity = 20.0f;
    8.     private float Rotation = 15.0f;
    9.     private int forceBrake = 70;
    10.  
    11.     //Wheels
    12.     public WheelCollider WheelBackL;
    13.     public WheelCollider WheelBackR;
    14.     public WheelCollider WheelFrontL;
    15.     public WheelCollider WheelFrontR;
    16.  
    17.     void Start()
    18.     {
    19.         this.rigidbody.centerOfMass = new Vector3(0, 0, 0);
    20.     }
    21.  
    22.     // Update
    23.     void FixedUpdate () {
    24.         //Move forward and backward
    25.         WheelBackL.motorTorque = -Velocity * Input.GetAxis("Vertical");
    26.         WheelBackR.motorTorque = -Velocity * Input.GetAxis("Vertical");
    27.         //Turn left and right
    28.         WheelFrontL.steerAngle = Rotation * Input.GetAxis("Horizontal");
    29.         WheelFrontR.steerAngle = Rotation * Input.GetAxis("Horizontal");
    30.         //Brake
    31.         if (Input.GetKey("space"))
    32.         {
    33.             WheelBackL.brakeTorque = forceBrake;
    34.             WheelBackR.brakeTorque = forceBrake;
    35.         }
    36.         else
    37.         {
    38.             WheelBackL.brakeTorque = 6;
    39.             WheelBackR.brakeTorque = 6;
    40.         }
    41.         //Automatic reset direction
    42.         if (!Input.GetKey("a")  !Input.GetKey("d"))
    43.         {
    44.             WheelFrontL.steerAngle = 0;
    45.             WheelFrontR.steerAngle = 0;
    46.         }
    47.     }
    48. }
    49.  
    50.  
    Does anyone know why?
    Thanks
     
  2. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Well if you are changing the center of mass, of course its going to effect your car. You set it at 0,0,0 which is probably wrong, the physics engine will calculate the COM automatically. The usual thing to do is lower the Center Of Mass.

    Code (csharp):
    1. public Vector3 comOffset = new Vector3(0,-1,0);
    2.  
    3.  
    4. Vector3 com = rigidbody.centerOfMass;
    5. com -= comOffset;
    6. rigidbody.centerOfMass = com;
     
  3. DarkSoul

    DarkSoul

    Joined:
    Dec 24, 2009
    Posts:
    37
    The problem continue. This time I created a empty object and I have added the mass in this object, but nothing.
    Code (csharp):
    1.  
    2. public Transform Rwheels;
    3.  
    4.     void Start()
    5.     {
    6.         this.rigidbody.centerOfMass = Rwheels.localPosition;
    7.     }
     
  4. JeffAU

    JeffAU

    Joined:
    Mar 22, 2009
    Posts:
    387
    Heya Darksoul,

    You may like to check out our GTA4 Vehicle Replica Project.

    The project currently has 24 contributors and a budget of $2600 which is increasing every day. Even Unity has contributed to the budget. There are currently a few developers working on the project and the developer who can create a GTA4 Replica vehicle in terms of handling (burnouts etc) will get the total budget for their efforts.

    It would be great to have you on board. No need to make a payment yet, just make a pledge min: $50 to guarantee your stake in the final project.

    Cheers


    Jeff