Search Unity

Vehicle scripting - checking that wheels are on the ground

Discussion in 'Scripting' started by pdrummond, Jul 4, 2005.

  1. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    I've been experimenting with vehicles and now have a four wheel steering jeep I can drive around. However, I need to limit acceleration to situations where the wheels are on the ground. I had a look at the RaycastCar.cs script from the car playground demo and, while I think I understand the concept, I could really do with a Javascript example. Can anyone help?

    In my vehicle script I use four game object references to control my wheels:

    var wheelFrontLeft : GameObject;
    var wheelFrontRight : GameObject;
    var wheelBackLeft : GameObject;
    var wheelBackRight : GameObject;

    How do I check that these are currently in contact with something else? Any tips are gratefully recieved.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You create one bool for every wheel which you want to track if it is in contact.

    In FixedUpdate () you reset the bool of all wheels.

    Then you override OnCollisionStay
    function OnCollisionStay (info : Collision) { }

    In there you loop through all contacts and check if the thisCollider is the same as the wheel. If it is, you can enable the bool for that wheel.

    for (c in info.contacts) { c.thisCollider }

    Next time you are in FixedUpdate or Update, your bools will know which wheels collide and which don't.
     
    profitdefiant likes this.
  3. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    I tried that using the RaycastCar.cs script as a guide but got loads of errors. Is 'for (c in info.contacts)' the correct code to use in a Javascript script?
    I think I need an actual Javascript example to help me understand this. Is there any chance of getting a Javascript version of RaycastCar.cs? Sorry to be a pain, but without documentation I'm finding it hard going.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Here you go.
     

    Attached Files:

  5. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    Much appreciated. I'll let you know how I get on.
     
  6. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    I'm afraid I just can't get this to work. I even tried changing the car playground demo to use the javascript version of RaycastCar instead of the original one. It threw lots of errors and refused to work.
    When I try to use the OnCollisionStay function in my script Unity either complains about extra '}' characters or gives a general 'script error'. I'm probably being thick, but is the RaycastCar.js file completely written in javascript? I ask because the following doesn't look quite like javascript to me:

    function OnCollisionStay (Collision collision) {
    foreach (ContactPoint c in collision.contacts)
    {
    Transform thisTransform = c.thisCollider.transform;
    if (thisTransform == backLeftWheel || thisTransform == backRightWheel)
    hasWheelContact = true;
    }
    hasGroundContact = true;
    }

    The variable types seem to be declared as if the language being used is actually C# or similar. E.g. 'Transform thisTransform'. I thought "Ah, I'll just take out the type declarations and make it like javascript." When I tried that I got errors such as 'The message parameter has to be of type: P7Contact.'

    I'm probably making really obvious mistakes, but it's very hard without documentation. If you can point me in the right direction I'd appreciate it.

    I've attached my vehicle script. Try not to laugh at it :)
     

    Attached Files:

  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You are using the first version of the script i uploaded.
    After that i uploaded a new version which i tested a bit and it worked when I attached it to the car in the car demo.
    Sorry for not having made it clearer, that i uploaded a new version of the script.

    If you refetch the script i have attached, it should just work.
    It does not contain a foreach statement anymore.
     
  8. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    Thanks for that. The code looks much clearer now. I'll give a go.