duck

duck

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