1 using System;
2 using UnityEngine;
3
4 namespace UnityStandardAssets.Vehicles.Car
5 {
6 public class CarSelfRighting : MonoBehaviour
7 {
8 // Automatically put the car the right way up, if it has come to rest upside-down.
9 [SerializeField] private float m_WaitTime = 3f; // time to wait before self righting
10 [SerializeField] private float m_VelocityThreshold = 1f; // the velocity below which the car is considered stationary for self-righting
11
12 private float m_LastOkTime; // the last time that the car was in an OK state
13 private Rigidbody m_Rigidbody;
14
15
16 private void Start()
17 {
18 m_Rigidbody = GetComponent<Rigidbody>();
19 }
20
21
22 private void Update()
23 {
24 // is the car is the right way up
25 if (transform.up.y > 0f || m_Rigidbody.velocity.magnitude > m_VelocityThreshold)
26 {
27 m_LastOkTime = Time.time;
28 }
29
30 if (Time.time > m_LastOkTime + m_WaitTime)
31 {
32 RightCar();
33 }
34 }
35
36
37 // put the car back the right way up:
38 private void RightCar()
39 {
40 // set the correct orientation for the car, and lift it off the ground a little
41 transform.position += Vector3.up;
42 transform.rotation = Quaternion.LookRotation(transform.forward);
43 }
44 }
45 }
2 using UnityEngine;
3
4 namespace UnityStandardAssets.Vehicles.Car
5 {
6 public class CarSelfRighting : MonoBehaviour
7 {
8 // Automatically put the car the right way up, if it has come to rest upside-down.
9 [SerializeField] private float m_WaitTime = 3f; // time to wait before self righting
10 [SerializeField] private float m_VelocityThreshold = 1f; // the velocity below which the car is considered stationary for self-righting
11
12 private float m_LastOkTime; // the last time that the car was in an OK state
13 private Rigidbody m_Rigidbody;
14
15
16 private void Start()
17 {
18 m_Rigidbody = GetComponent<Rigidbody>();
19 }
20
21
22 private void Update()
23 {
24 // is the car is the right way up
25 if (transform.up.y > 0f || m_Rigidbody.velocity.magnitude > m_VelocityThreshold)
26 {
27 m_LastOkTime = Time.time;
28 }
29
30 if (Time.time > m_LastOkTime + m_WaitTime)
31 {
32 RightCar();
33 }
34 }
35
36
37 // put the car back the right way up:
38 private void RightCar()
39 {
40 // set the correct orientation for the car, and lift it off the ground a little
41 transform.position += Vector3.up;
42 transform.rotation = Quaternion.LookRotation(transform.forward);
43 }
44 }
45 }