duck

duck

Jan 28th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Car : MonoBehaviour {
  5.  
  6.     Vector3 targetVelocity = Vector3.zero;
  7.     float turnVelocity = 0;
  8.     float maxSpeed = 100;
  9.     float accelerationSpeed = 0.1f;
  10.     float turnSpeed = 0.1f;
  11.     bool onGround;
  12.     Vector3 oPosition;
  13.     Quaternion oRotation;
  14.  
  15.     void Start () {
  16.         rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
  17.         oPosition = transform.position;
  18.         oRotation = transform.rotation;
  19.     }
  20.  
  21.     void OnCollisionStay(Collision col)
  22.     {
  23.         if (Vector3.Angle(transform.up, Vector3.up) < 65)
  24.         {
  25.             onGround = true;
  26.         }
  27.     }
  28.  
  29.     void FixedUpdate()
  30.     {
  31.         if (onGround)
  32.         {
  33.             Vector3 newVelocity = Vector3.Lerp(rigidbody.velocity, targetVelocity, Time.fixedDeltaTime * 3);
  34.             rigidbody.velocity = new Vector3(newVelocity.x, rigidbody.velocity.y, newVelocity.z);
  35.             rigidbody.angularVelocity = new Vector3(rigidbody.angularVelocity.x, turnVelocity, rigidbody.angularVelocity.z);
  36.         }
  37.         rigidbody.WakeUp();
  38.         onGround = false;
  39.     }
  40.  
  41.     void Update () {
  42.         if (Input.GetKeyDown("r")) { Reset(); }
  43.         float v = Input.GetAxis("Vertical");
  44.         float h = Input.GetAxis("Horizontal");
  45.         targetVelocity = transform.TransformDirection(new Vector3(0, 0, Mathf.Lerp(rigidbody.velocity.magnitude, v * maxSpeed, accelerationSpeed)));
  46.         turnVelocity = h * Vector3.Dot(rigidbody.velocity, transform.forward) * turnSpeed * (1.2f-Mathf.Clamp01(rigidbody.velocity.magnitude/maxSpeed));
  47.      }
  48.  
  49.     void Reset()
  50.     {
  51.         transform.position = oPosition;
  52.         transform.rotation = oRotation;
  53.         rigidbody.velocity = Vector3.zero;
  54.         rigidbody.angularVelocity = Vector3.zero;
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment