Trying to make car flip 360

Hi

i am trying to make the car flip 360 in the air when its jump of the road or edge my problem is the car is stuck always on 20dgree or -20 dgree its dose not flip what ever i did , i am still new to unity :smile: any help here is very much apprciated

my Script

public class CarController : MonoBehaviour
{

    public GameObject lanePoints;
    public GameObject[] carWheels;

    public static CarController Instance { set; get; }
    private const float LANE_DISTANCE = 4.0f;
    private const float TURN_SPEED = 2f;
    private float diseredLane = 1.0f; // 0 left 1 middle 2 right
    private Rigidbody rb;
    private float speed = 0f;
    private float speedTime = 15f;
    float rotation = 0f;

    public void Start()
    {
        CarController.Instance = this;

        rb = GetComponent<Rigidbody>();
    }

    public float getCurrentSpeed()
    {
        return speed;
    }

    public void startWithSpeed(float setSpeed, float Multiplayer)
    {
        print("[PLAYER MOTOR] start game with Speed " + setSpeed + " Multiplayer " + Multiplayer);

        this.speed                 = setSpeed * Multiplayer;
        this.speedTime             = 1.5f * Multiplayer;

        if ( transform.position.x < -2.5f)
        {
            diseredLane = 0f;
        }else
        if (transform.position.x > 2.5f)
        {
            diseredLane = 2f;
        }

        StartCoroutine(ChangeSpeed(this.speed));
    }

    IEnumerator ChangeSpeed(float v_start)
    {
        float elapsed = 0.0f;

        while (elapsed < this.speedTime)
        {
            speed     = Mathf.Lerp(v_start, 0f, elapsed / this.speedTime);
            elapsed += Time.deltaTime;
            yield return null;
        }

        speed = 0f;
    }

    // Update is called once per frame
    private void Update()
    {

        //GAME NOT YET STARTED
        if (LevelManager.Instance.isGameStarted == false)
        {
            return;
        }

        LevelManager.Instance.speed = this.speed;

        if (this.speed == 0)
        {
            return;
        }

        if (MobileInput.Instance.SwipeLeft)
        {
            this.MoveLane(false);
        }else
        if (MobileInput.Instance.SwipeRight)
        {
            this.MoveLane(true);
        }

        Vector3 targetPosition = transform.position.z * Vector3.forward;

        if (diseredLane == 0)
        {
            targetPosition += Vector3.left * LANE_DISTANCE;
        }else
        if (diseredLane == 2)
        {
            targetPosition += Vector3.right * LANE_DISTANCE;
        }

        //Calculate move vector Delta
        Vector3 moveVector = Vector3.zero;
                moveVector.x = (targetPosition - transform.position).normalized.x * speed ;
                moveVector.y = rb.velocity.y;
                moveVector.z = Vector3.forward.z * speed;

        if ( this.speed > 0 )
        {
            rotation = ( 15f * (targetPosition - transform.position).normalized.x);

            this.carWheels[0].transform.localEulerAngles = new Vector3(this.carWheels[0].transform.rotation.x - ( speed * 60.5f), rotation, 0f);
            this.carWheels[1].transform.localEulerAngles = this.carWheels[0].transform.localEulerAngles;
            this.carWheels[2].transform.localEulerAngles = new Vector3(this.carWheels[0].transform.rotation.x - (speed * 60.5f), 0f, 0f);
            this.carWheels[3].transform.localEulerAngles = this.carWheels[2].transform.localEulerAngles;

            transform.rotation = Quaternion.Lerp(transform.rotation,
                                                  Quaternion.Euler(transform.eulerAngles.x, (25f * (targetPosition - transform.position).normalized.x), 0f),
                                                 Time.deltaTime * 14f);

            transform.Translate(moveVector * Time.deltaTime, Space.World);
        }

    }



    void FixedUpdate()
    {
        //Limit Speed
        if (rb.velocity.z > speed)
        {
            rb.velocity = rb.velocity.normalized * speed;
        }
    }


    private void MoveLane(bool goingRight)
    {
        diseredLane += (goingRight) ? 1 : -1;
        diseredLane = Mathf.Clamp(diseredLane, 0, 2);
    }

}

I’m sorry, I don’t see any flip code above. Perhaps you could draw our attention to it?

How to report problems correctly in the Unity3D forums:

http://plbm.com/?p=220

@Kurt-Dekker i don’t want to flip 360 , i want to flip the car more than or less than 360 its depends on the cliff high + speed of the car ( Rigidbody angular ) , the problem is what ever the speed is slow or fast or the cliff high or low the car is not rotating like normal physics flipping while jumping in the air

Hi,

Dont think normal cars will make full backflips by themselves either without serious manipulation of the center of gravity. You propably need to stimulate the rotational mass of the wheels or something too.
Here are some idéas of what´s going on:

It´s probably easier to just rotate the car on the x-axis when you want it to make a backflip, if it´s for a game.

@arfish i have cars have wight on the back and front , and the flip depends on the gravity , my issue is the X rotation , when the car start to flip its stuck on 20dgree but when i comment the line

  • transform.rotation = Quaternion.Lerp(transform.rotation,
  • Quaternion.Euler(transform.eulerAngles.x, (25f * (targetPosition - transform.position).normalized.x), 0f),
  • Time.deltaTime * 14f);

the car do full flip or rotating

Perhaps you could add some torque to the car when accelerating, or jumping, to simulate the rotational mass of the wheels.