Komak57

HoverController.cs

May 8th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /* XXXXXXXXXXXXXXXXXXX
  5.  * XX Generated by: XX
  6.  * XX      Komak57  XX
  7.  * XXXXXXXXXXXXXXXXXXX
  8.  */
  9. public class HoverController : MonoBehaviour {
  10.     // publicly used variables
  11.     public Vector2 MouseSensitivity = new Vector2(1.0f,1.0f);
  12.     public float CamDist = 5.0f;
  13.     public float VertCamClampAngle = 80.0f;
  14.     public List<string> CamCollisionTags = new List<string>();
  15.    
  16.     public float Gravity = -9.8f;
  17.     public float MaxThrust = 10.0f;
  18.     public float MaxSpeed = 10.0f;
  19.    
  20.     public bool CanJump = true;
  21.     public float JumpHeight = 7.0f;
  22.     public bool CanDoubleJump = true;
  23.     public float DoubleJumpHeight = 7.0f;
  24.    
  25.     // privately used variables
  26.     //public float curSpeed = 0.0f;
  27.     float verticalForce = 0;
  28.     Vector3 AirVelocity = Vector3.zero;
  29.     private float pitch = 10.0f;
  30.     private float yaw = -90.0f;
  31.     Vector3 momentum = Vector3.zero;
  32.     float SpeedDamper = 1.0f; // based on percent 0-1
  33.     int jumpcount = 0;
  34.     CharacterController plyr;
  35.     Animator anim;
  36.    
  37.     // Use this for initialization
  38.     void Start () {
  39.         Screen.lockCursor = true;
  40.         plyr = GetComponent<CharacterController>();
  41.         anim = GetComponentInChildren<Animator>();//GetComponent<Animator>();
  42.         if (!anim)
  43.             Debug.LogWarning("No animator component attached to "+gameObject.name);
  44.     }
  45.     bool wasGrounded = false;
  46.     bool isGrounded = false;
  47.     Vector3 lastdir = Vector3.zero;
  48.     Vector3 lastmov = Vector3.zero;
  49.     Vector3 movement = Vector3.zero;
  50.     // Update is called once per frame
  51.     void Update () {
  52.         wasGrounded = isGrounded;
  53.         // manual ground check to increase distance before airborn
  54.         isGrounded = ((plyr.transform.position-GetGroundPos()).magnitude < 1.1);
  55.         //Debug.Log("Distance from ground = "+(plyr.transform.position-GetGroundPos()).magnitude);
  56.         // update camera direction
  57.         float oldyaw = yaw;
  58.         yaw += Input.GetAxis("Mouse X") * MouseSensitivity.x * Time.deltaTime;
  59.         transform.Rotate(new Vector3(0, yaw-oldyaw, 0)); // local rotation
  60.         pitch += Input.GetAxis("Mouse Y") * MouseSensitivity.y * Time.deltaTime;
  61.         pitch = Mathf.Clamp(pitch, -VertCamClampAngle, VertCamClampAngle);
  62.         // snap camera for movement adjustments
  63.         SnapCam();
  64.        
  65.         // Get desired direction with accelleration modifiers
  66.         Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
  67.         // Direction Translators
  68.         Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
  69.         forward.y = 0; forward = forward.normalized;
  70.         Vector3 right = new Vector3(forward.z, 0, -forward.x);
  71.        
  72.         // only accellerate/decellerate on ground
  73.         if (isGrounded) {
  74.             // reset jump count on the ground
  75.             jumpcount = 0;
  76.         }
  77.        
  78.         // if we can jump, and we pressed the jump button, and we are grounded, or we can double jump AND we haven't reached our jump cap
  79.         if (CanJump && Input.GetButtonDown("Jump") && (plyr.isGrounded || (CanDoubleJump && jumpcount <= 1))) {
  80.             if (jumpcount == 0)
  81.                 verticalForce = JumpHeight;
  82.             if (jumpcount == 1)
  83.                 verticalForce = DoubleJumpHeight;
  84.             jumpcount++;
  85.         }
  86.        
  87.         // save last moement direction
  88.         if (input.magnitude > 0.2f) {
  89.             // allow adjustments and save result
  90.             lastmov = movement;
  91.         }
  92.        
  93.         movement = (input.z * right + input.x * forward);
  94.         movement = Vector3.ClampMagnitude(movement, MaxThrust * SpeedDamper);
  95.         if (!isGrounded) { // Airborn
  96.             if (wasGrounded) // lock airvelocity
  97.                 AirVelocity = movement;
  98.             // low movement control in air
  99.             movement = AirVelocity + (movement * 0.1f);
  100.         } else {
  101.             AirVelocity = Vector3.zero;
  102.             // still-deceleration on ground
  103.             if (input.magnitude == 0 || momentum.magnitude > 0.1) { // trying to stop with momentum
  104.                 // decelerate 10%
  105.                 movement = -Vector3.ClampMagnitude(momentum, (MaxSpeed*0.1f)*Time.deltaTime);
  106.                 Debug.Log("Trying to stop with a momentum of "+momentum.magnitude+", using "+movement.magnitude+" deceleration.");
  107.             }
  108.         }
  109.         momentum += movement;
  110.         momentum = Vector3.ClampMagnitude(momentum, MaxSpeed * SpeedDamper * Time.deltaTime);
  111.  
  112.         // force player in direction
  113.         //plyr.transform.position = plyr.transform.position + momentum;
  114.         plyr.Move(momentum);
  115.         // Drop player
  116.         ApplyGravity();
  117.     }
  118.     public void SetSlow(float speed) {
  119.         SpeedDamper = speed;
  120.     }
  121.     public void SetRotationLock(bool toggle) {
  122.     }
  123.    
  124.     void SnapCam() {
  125.         Vector3 camDestination = CollidePoint(transform.position, GetCamPos());
  126.  
  127.         Camera.main.transform.position = camDestination;
  128.         Camera.main.transform.LookAt(transform.position);
  129.     }
  130.     Vector3 GetCamPos() {
  131.         Vector3 tmp = Vector3.zero;
  132.         float t = (float)(CamDist * Mathf.Cos(ToRadian(pitch)));
  133.         tmp.y = (float)(CamDist * Mathf.Sin(ToRadian(pitch)) + transform.position.y);
  134.  
  135.         tmp.x = (float)(t * Mathf.Sin(ToRadian(yaw)) + transform.position.x);
  136.         //tmp.Y = (float)this.pos.Y;
  137.         tmp.z = (float)(t * Mathf.Cos(ToRadian(yaw)) + transform.position.z);
  138.         return tmp;
  139.     }
  140.    
  141.     float ToRadian(float angle)
  142.     {
  143.         return 3.14f * angle / 180.0f;
  144.     }
  145.    
  146.     // Collision detection for camera on closest non-player object
  147.     Vector3 CollidePoint(Vector3 begin, Vector3 target) {
  148.         RaycastHit[] hits;
  149.         hits = Physics.RaycastAll(begin, (target-begin), (target-begin).magnitude);
  150.         Vector3 closest = target;
  151.         foreach (RaycastHit hit in hits) {
  152.             Vector3 loc = (hit.point + (hit.normal * 0.01f));
  153.             if (CamCollisionTags.Contains(hit.collider.transform.tag))
  154.                 if ((begin-loc).magnitude < (begin-closest).magnitude)
  155.                     closest = loc;
  156.         }
  157.         return closest;
  158.     }
  159.    
  160.     // Collision detection for camera on closest non-player object
  161.     Vector3 GetGroundPos() {
  162.         Vector3 begin = plyr.transform.position;
  163.         RaycastHit[] hits;
  164.         hits = Physics.RaycastAll(begin, Vector3.down);
  165.         Vector3 closest = Vector3.zero;
  166.         foreach (RaycastHit hit in hits) {
  167.             Vector3 loc = (hit.point + (hit.normal * 0.1f));
  168.             if (hit.collider.transform.tag == "Environment")
  169.                 if ((begin-loc).magnitude < (begin-closest).magnitude || closest == Vector3.zero)
  170.                     closest = loc;
  171.         }
  172.         return closest;
  173.     }
  174.    
  175.     void ApplyGravity() {
  176.         // constant gravitational force
  177.         verticalForce += Gravity * Time.deltaTime;
  178.  
  179.         // apply normal gravity
  180.         plyr.Move((new Vector3(0, verticalForce, 0)*Time.deltaTime));
  181.  
  182.         if (plyr.isGrounded) {
  183.             // Animation plays when we hit the ground with a force > 2
  184.             if (Mathf.Abs(verticalForce) >= 2.0f) {
  185.                 anim.SetBool("Bounce", true);
  186.                 // invert animation speed just a little to accomodate for heavier drops
  187.                 anim.speed = 3.0f-Mathf.Clamp(Mathf.Abs(verticalForce), 0.0f, 2.2f);
  188.             } else {
  189.                 anim.SetBool("Bounce", false);
  190.             }
  191.             // reset force to prevent reanimations
  192.             verticalForce = 0;
  193.         } else {
  194.             anim.SetBool("Bounce", false);
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment