Advertisement
Guest User

Untitled

a guest
Mar 29th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu("Kiz Custom/Rigidbody Character Controller")]
  5. [RequireComponent(typeof(Rigidbody))]
  6. [RequireComponent(typeof(Collider))]
  7.  
  8. public class PhysicsController : MonoBehaviour {
  9.     Rigidbody RigidbodyComponent = null;
  10.     CapsuleCollider CapsuleComponent = null;
  11.     Transform CachedTransform = null;
  12.  
  13.     public float SlopeLimit = 45.0f;
  14.     public Vector3 GravityDirection = new Vector3(0.0f, -1.0f, 0.0f);
  15.  
  16.     [HideInInspector]
  17.     public bool IsGrounded = false;
  18.     protected Vector3 movementAccumulator = Vector3.zero;
  19.     protected Vector3 jumpAccumulator = Vector3.zero;
  20.     protected Vector3 correctionAccumulator = Vector3.zero;
  21.     public const float Gravity = 9.8f;
  22.     protected float ReferenceCos;
  23.     protected LayerMask ignoreLayer;
  24.  
  25.     public Vector3 Velocity {
  26.         get { return RigidbodyComponent.velocity; }
  27.         set { RigidbodyComponent.velocity = value; }
  28.     }
  29.  
  30.     public Vector3 DesiredVelocity {
  31.         get { return movementAccumulator; }
  32.         set { movementAccumulator = value; }
  33.     }
  34.  
  35.     public Vector3 JumpVelocity {
  36.         get { return jumpAccumulator; }
  37.         set { jumpAccumulator = value; }
  38.     }
  39.  
  40.     void Start() {
  41.         RigidbodyComponent = GetComponent<Rigidbody>();
  42.         CapsuleComponent = GetComponent<CapsuleCollider>();
  43.         CachedTransform = GetComponent<Transform>();
  44.  
  45.         if (RigidbodyComponent == null || CapsuleComponent == null) {
  46.             if (RigidbodyComponent == null)
  47.                 Debug.LogError("[" + gameObject.name + "] CustomController: Rigidbody component required!");
  48.             if (CapsuleComponent == null)
  49.                 Debug.LogError("[" + gameObject.name + "] CustomController: CapsuleCollider component required!");
  50.         }
  51.  
  52.         RigidbodyComponent.freezeRotation = true;
  53.         RigidbodyComponent.useGravity = false;
  54.         RigidbodyComponent.isKinematic = false;
  55.         CapsuleComponent.isTrigger = false;
  56.         ReferenceCos = Mathf.Cos(SlopeLimit);
  57.  
  58.         ignoreLayer = 1 << LayerMask.NameToLayer("Player");
  59.         ignoreLayer = ~ignoreLayer;
  60.     }
  61.  
  62.     public void Move(Vector3 moveVector) {
  63.         movementAccumulator += moveVector;
  64.     }
  65.  
  66.     public void Jump(Vector3 jumpVector) {
  67.         jumpAccumulator += jumpVector;
  68.     }
  69.  
  70.     void FixedUpdate() {
  71.         UpdateVelocity(movementAccumulator, jumpAccumulator);
  72.         movementAccumulator = Vector3.zero;
  73.         jumpAccumulator = Vector3.zero;
  74.         IsGrounded = false;
  75.     }
  76.  
  77.     void LateUpdate() {
  78.         if (CachedTransform.up != -1.0f * GravityDirection)
  79.             CachedTransform.up = -1.0f * GravityDirection;
  80.     }
  81.  
  82.     void UpdateVelocity(Vector3 desiredVelocity, Vector3 jumpVelocity) {
  83.         float Distance = CapsuleComponent.radius * 2.0f;
  84.         RaycastHit hitInfo;
  85.  
  86.         if (Physics.Raycast(CachedTransform.position, desiredVelocity.normalized, out hitInfo, Distance, ignoreLayer))
  87.             desiredVelocity = hitInfo.normal * hitInfo.distance;
  88.  
  89.         Vector3 VelocityDelta = desiredVelocity - RigidbodyComponent.velocity;
  90.         Vector3 AntiGravity = GravityDirection * DotProduct(VelocityDelta, GravityDirection) * -1.0f;
  91.  
  92.         RigidbodyComponent.AddForce(VelocityDelta + AntiGravity + jumpVelocity, ForceMode.VelocityChange);
  93.         RigidbodyComponent.AddForce(GravityDirection * Gravity * RigidbodyComponent.mass * Time.deltaTime, ForceMode.VelocityChange);
  94.     }
  95.  
  96.     protected static Vector3 Vec3Mul(Vector3 v1, Vector3 v2) {
  97.         return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
  98.     }
  99.  
  100.     protected static float DotProduct(Vector3 v1, Vector3 v2) {
  101.         return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  102.     }
  103.  
  104.     void TrackGrounded(Collision collision) {
  105.         foreach (ContactPoint contact in collision.contacts) {
  106.             Vector3 LocalHitNormal = contact.normal;
  107.             if (Vector3.Dot(CachedTransform.up, LocalHitNormal) <= ReferenceCos) {
  108.                 // TODO: MAYBE SLIDE?
  109.             } else // if (contact.otherCollider.tag == "ground")
  110.                 IsGrounded = true;
  111.         }
  112.     }
  113.  
  114.     void OnCollisionExit(Collision collision) {
  115.         IsGrounded = false;
  116.     }
  117.  
  118.     void OnCollisionStay(Collision collision) {
  119.         TrackGrounded(collision);
  120.     }
  121.  
  122.     void OnCollisionEnter(Collision collision) {
  123.         TrackGrounded(collision);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement