duck

duck

Jan 28th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CarBetter : MonoBehaviour {
  5.  
  6.     public float maxSpeed = 100;
  7.     public float enginePower = 20;
  8.     public float minTurnForce = 0.1f;
  9.     public float maxTurnForce = 0.6f;
  10.     public float lateralGrip = 3;
  11.  
  12.     bool onGround;
  13.     Vector3 oPosition;
  14.     Quaternion oRotation;
  15.     float h,v;
  16.    
  17.     void Start () {
  18.         oPosition = transform.position;
  19.         oRotation = transform.rotation;
  20.         collider.material.dynamicFriction = 0;
  21.         collider.material.staticFriction = 0;
  22.         rigidbody.angularDrag = 2;
  23.         rigidbody.drag = 0.1f;
  24.     }
  25.    
  26.  
  27.     void OnCollisionStay(Collision col)
  28.     {
  29.         if (Vector3.Angle(transform.up, Vector3.up) < 65)
  30.         {
  31.             onGround = true;
  32.         }
  33.     }
  34.  
  35.     void FixedUpdate()
  36.     {
  37.         if (onGround)
  38.         {
  39.             Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);    
  40.            
  41.             float currentSpeed = localVelocity.z;
  42.             float inverseSpeedFactor = Mathf.InverseLerp( maxSpeed, 0, Mathf.Abs(currentSpeed) );
  43.             float accelPower = enginePower * inverseSpeedFactor;
  44.            
  45.             Vector3 accelForce = transform.forward * accelPower * v;
  46.             Vector3 gripForce = -transform.right * lateralGrip * localVelocity.x;
  47.             Vector3 sumForce = accelForce + gripForce;
  48.             Vector3 turnTorque = currentSpeed * transform.up * h * Mathf.Lerp(minTurnForce, maxTurnForce, inverseSpeedFactor);
  49.            
  50.             rigidbody.AddForce( sumForce );
  51.             rigidbody.AddTorque( turnTorque );
  52.         }
  53.         onGround = false;
  54.     }
  55.  
  56.     void Update () {
  57.         if (Input.GetKeyDown("r")) { Reset(); }
  58.         v = Input.GetAxis("Vertical");
  59.         h = Input.GetAxis("Horizontal");
  60.     }
  61.  
  62.     void Reset()
  63.     {
  64.         transform.position = oPosition;
  65.         transform.rotation = oRotation;
  66.         rigidbody.velocity = Vector3.zero;
  67.         rigidbody.angularVelocity = Vector3.zero;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment