Search Unity

Resetting AI car after it flips over

Discussion in 'Scripting' started by iceherosubzero, Aug 28, 2009.

  1. iceherosubzero

    iceherosubzero

    Joined:
    Jul 10, 2009
    Posts:
    36
    I am working on a car racing game. For resetting a car if it flips over I am using the code given below. It works perfectly for player car by pressing "r" as input. But if player car collides with AI car the AI car flips over the resetting should happen automatically.

    I am saving the AI cars transform every frame till it is on the ground. When it flips over I am giving the position from this saved transform so that it gets there after resetting.I am using Quaternion.identity for rotation(refer below code). But the AIcar after resetting remains in its previous state(wheels moving and everything) which is leading to wierd behaviour after reset.
    Is there any way to neutralize everything related to the car(all its physics related parameters) during reset?

    Also is there any way to use Sleep() functionality for stopping execution of a particular piece of code for some time?


    the code for reset:

    rigidbody.velocity = new Vector3(0f, 0f, 0f);
    Vector3 a = transform.localRotation.eulerAngles;
    a.x = 0;
    a.y = Mathf.Repeat(a.y + Input.GetAxis("Horizontal") * 5f, 360f);
    a.z = 0;
    transform.localRotation = Quaternion.Euler(a);
    transform.localPosition = savedtransform.position + Vector3.up;
     
  2. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Yes, there is a way :), make your rigidbody kinematic, being kinematic makes the object unaffected by physics :)

    deactivate the object/script could deactivate its functionality :)

    Hope it helps! ;)
     
  3. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
  4. divineelite

    divineelite

    Joined:
    Aug 12, 2009
    Posts:
    48
    Thanks for the replies guys. Making the object kinematic is working wonderfully :D

    For the sleep thing I have currently used a counter which is doing the job but I think Ill look into coroutines as well. It is a better way of doing things(less bugs :p)

    I am a colleague of iceherosubzero btw!