Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Car : MonoBehaviour {
- Vector3 targetVelocity = Vector3.zero;
- float turnVelocity = 0;
- float maxSpeed = 100;
- float accelerationSpeed = 0.1f;
- float turnSpeed = 0.1f;
- bool onGround;
- Vector3 oPosition;
- Quaternion oRotation;
- void Start () {
- rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
- oPosition = transform.position;
- oRotation = transform.rotation;
- }
- void OnCollisionStay(Collision col)
- {
- if (Vector3.Angle(transform.up, Vector3.up) < 65)
- {
- onGround = true;
- }
- }
- void FixedUpdate()
- {
- if (onGround)
- {
- Vector3 newVelocity = Vector3.Lerp(rigidbody.velocity, targetVelocity, Time.fixedDeltaTime * 3);
- rigidbody.velocity = new Vector3(newVelocity.x, rigidbody.velocity.y, newVelocity.z);
- rigidbody.angularVelocity = new Vector3(rigidbody.angularVelocity.x, turnVelocity, rigidbody.angularVelocity.z);
- }
- rigidbody.WakeUp();
- onGround = false;
- }
- void Update () {
- if (Input.GetKeyDown("r")) { Reset(); }
- float v = Input.GetAxis("Vertical");
- float h = Input.GetAxis("Horizontal");
- targetVelocity = transform.TransformDirection(new Vector3(0, 0, Mathf.Lerp(rigidbody.velocity.magnitude, v * maxSpeed, accelerationSpeed)));
- turnVelocity = h * Vector3.Dot(rigidbody.velocity, transform.forward) * turnSpeed * (1.2f-Mathf.Clamp01(rigidbody.velocity.magnitude/maxSpeed));
- }
- void Reset()
- {
- transform.position = oPosition;
- transform.rotation = oRotation;
- rigidbody.velocity = Vector3.zero;
- rigidbody.angularVelocity = Vector3.zero;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment