Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class CarBetter : MonoBehaviour {
- public float maxSpeed = 100;
- public float enginePower = 20;
- public float minTurnForce = 0.1f;
- public float maxTurnForce = 0.6f;
- public float lateralGrip = 3;
- bool onGround;
- Vector3 oPosition;
- Quaternion oRotation;
- float h,v;
- void Start () {
- oPosition = transform.position;
- oRotation = transform.rotation;
- collider.material.dynamicFriction = 0;
- collider.material.staticFriction = 0;
- rigidbody.angularDrag = 2;
- rigidbody.drag = 0.1f;
- }
- void OnCollisionStay(Collision col)
- {
- if (Vector3.Angle(transform.up, Vector3.up) < 65)
- {
- onGround = true;
- }
- }
- void FixedUpdate()
- {
- if (onGround)
- {
- Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
- float currentSpeed = localVelocity.z;
- float inverseSpeedFactor = Mathf.InverseLerp( maxSpeed, 0, Mathf.Abs(currentSpeed) );
- float accelPower = enginePower * inverseSpeedFactor;
- Vector3 accelForce = transform.forward * accelPower * v;
- Vector3 gripForce = -transform.right * lateralGrip * localVelocity.x;
- Vector3 sumForce = accelForce + gripForce;
- Vector3 turnTorque = currentSpeed * transform.up * h * Mathf.Lerp(minTurnForce, maxTurnForce, inverseSpeedFactor);
- rigidbody.AddForce( sumForce );
- rigidbody.AddTorque( turnTorque );
- }
- onGround = false;
- }
- void Update () {
- if (Input.GetKeyDown("r")) { Reset(); }
- v = Input.GetAxis("Vertical");
- h = Input.GetAxis("Horizontal");
- }
- 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