duck

duck

Jan 28th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 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 = 20;
  9.     public float minTurnForce = 0.2f;
  10.     public float maxTurnForce = 0.6f;
  11.     public float lateralGrip = 3;
  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.         collider.material.dynamicFriction = 0;
  25.         collider.material.staticFriction = 0;
  26.         rigidbody.angularDrag = 2;
  27.         rigidbody.drag = 0.1f;
  28.        
  29.     }
  30.    
  31.  
  32.     void OnCollisionStay(Collision col)
  33.     {
  34.         if (Vector3.Angle(transform.up, Vector3.up) < 65)
  35.         {
  36.             onGround = true;
  37.         }
  38.     }
  39.  
  40.     void FixedUpdate()
  41.     {
  42.         if (onGround)
  43.         {
  44.             Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
  45.            
  46.             float currentSpeed = Mathf.Abs(localVelocity.z);
  47.             float inverseSpeedFactor = Mathf.InverseLerp( maxSpeed, 0, currentSpeed );
  48.            
  49.             float accelPower = enginePower * inverseSpeedFactor;
  50.            
  51.             Vector3 accelForce = transform.forward * accelPower * v;
  52.             Vector3 gripForce = -transform.right * lateralGrip * localVelocity.x;
  53.             Vector3 sumForce = accelForce + gripForce;
  54.             Vector3 turnTorque = currentSpeed * transform.up * h * Mathf.Lerp(minTurnForce, maxTurnForce, inverseSpeedFactor);
  55.            
  56.             rigidbody.AddForce( sumForce );
  57.             rigidbody.AddTorque( turnTorque );
  58.            
  59.             Debug.DrawRay(transform.position, sumForce, Color.red);
  60.            
  61.  
  62.         }
  63.        
  64.         onGround = false;
  65.     }
  66.  
  67.     void Update () {
  68.         if (Input.GetKeyDown("r")) { Reset(); }
  69.         v = Input.GetAxis("Vertical");
  70.         h = Input.GetAxis("Horizontal");
  71.       }
  72.  
  73.     void Reset()
  74.     {
  75.         transform.position = oPosition;
  76.         transform.rotation = oRotation;
  77.         rigidbody.velocity = Vector3.zero;
  78.         rigidbody.angularVelocity = Vector3.zero;
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment