CarSelfRighting.cs 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
using System;
using UnityEngine;

namespace UnityStandardAssets.Vehicles.Car
{
    public class CarSelfRighting : MonoBehaviour
    {
        // Automatically put the car the right way up, if it has come to rest upside-down.
        [SerializeField] private float m_WaitTime = 3f;           // time to wait before self righting
        [SerializeField] private float m_VelocityThreshold = 1f;  // the velocity below which the car is considered stationary for self-righting

        private float m_LastOkTime; // the last time that the car was in an OK state
        private Rigidbody m_Rigidbody;


        private void Start()
        {
            m_Rigidbody = GetComponent<Rigidbody>();
        }


        private void Update()
        {
            // is the car is the right way up
            if (transform.up.y > 0f || m_Rigidbody.velocity.magnitude > m_VelocityThreshold)
            {
                m_LastOkTime = Time.time;
            }

            if (Time.time > m_LastOkTime + m_WaitTime)
            {
                RightCar();
            }
        }


        // put the car back the right way up:
        private void RightCar()
        {
            // set the correct orientation for the car, and lift it off the ground a little
            transform.position += Vector3.up;
            transform.rotation = Quaternion.LookRotation(transform.forward);
        }
    }
}