duck

duck

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