Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- /* XXXXXXXXXXXXXXXXXXX
- * XX Generated by: XX
- * XX Komak57 XX
- * XXXXXXXXXXXXXXXXXXX
- */
- public class HoverController : MonoBehaviour {
- // publicly used variables
- public Vector2 MouseSensitivity = new Vector2(1.0f,1.0f);
- public float CamDist = 5.0f;
- public float VertCamClampAngle = 80.0f;
- public List<string> CamCollisionTags = new List<string>();
- public float Gravity = -9.8f;
- public float MaxThrust = 10.0f;
- public float MaxSpeed = 10.0f;
- public bool CanJump = true;
- public float JumpHeight = 7.0f;
- public bool CanDoubleJump = true;
- public float DoubleJumpHeight = 7.0f;
- // privately used variables
- //public float curSpeed = 0.0f;
- float verticalForce = 0;
- Vector3 AirVelocity = Vector3.zero;
- private float pitch = 10.0f;
- private float yaw = -90.0f;
- Vector3 momentum = Vector3.zero;
- float SpeedDamper = 1.0f; // based on percent 0-1
- int jumpcount = 0;
- CharacterController plyr;
- Animator anim;
- // Use this for initialization
- void Start () {
- Screen.lockCursor = true;
- plyr = GetComponent<CharacterController>();
- anim = GetComponentInChildren<Animator>();//GetComponent<Animator>();
- if (!anim)
- Debug.LogWarning("No animator component attached to "+gameObject.name);
- }
- bool wasGrounded = false;
- bool isGrounded = false;
- Vector3 lastdir = Vector3.zero;
- Vector3 lastmov = Vector3.zero;
- Vector3 movement = Vector3.zero;
- // Update is called once per frame
- void Update () {
- wasGrounded = isGrounded;
- // manual ground check to increase distance before airborn
- isGrounded = ((plyr.transform.position-GetGroundPos()).magnitude < 1.1);
- //Debug.Log("Distance from ground = "+(plyr.transform.position-GetGroundPos()).magnitude);
- // update camera direction
- float oldyaw = yaw;
- yaw += Input.GetAxis("Mouse X") * MouseSensitivity.x * Time.deltaTime;
- transform.Rotate(new Vector3(0, yaw-oldyaw, 0)); // local rotation
- pitch += Input.GetAxis("Mouse Y") * MouseSensitivity.y * Time.deltaTime;
- pitch = Mathf.Clamp(pitch, -VertCamClampAngle, VertCamClampAngle);
- // snap camera for movement adjustments
- SnapCam();
- // Get desired direction with accelleration modifiers
- Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
- // Direction Translators
- Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
- forward.y = 0; forward = forward.normalized;
- Vector3 right = new Vector3(forward.z, 0, -forward.x);
- // only accellerate/decellerate on ground
- if (isGrounded) {
- // reset jump count on the ground
- jumpcount = 0;
- }
- // 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
- if (CanJump && Input.GetButtonDown("Jump") && (plyr.isGrounded || (CanDoubleJump && jumpcount <= 1))) {
- if (jumpcount == 0)
- verticalForce = JumpHeight;
- if (jumpcount == 1)
- verticalForce = DoubleJumpHeight;
- jumpcount++;
- }
- // save last moement direction
- if (input.magnitude > 0.2f) {
- // allow adjustments and save result
- lastmov = movement;
- }
- movement = (input.z * right + input.x * forward);
- movement = Vector3.ClampMagnitude(movement, MaxThrust * SpeedDamper);
- if (!isGrounded) { // Airborn
- if (wasGrounded) // lock airvelocity
- AirVelocity = movement;
- // low movement control in air
- movement = AirVelocity + (movement * 0.1f);
- } else {
- AirVelocity = Vector3.zero;
- // still-deceleration on ground
- if (input.magnitude == 0 || momentum.magnitude > 0.1) { // trying to stop with momentum
- // decelerate 10%
- movement = -Vector3.ClampMagnitude(momentum, (MaxSpeed*0.1f)*Time.deltaTime);
- Debug.Log("Trying to stop with a momentum of "+momentum.magnitude+", using "+movement.magnitude+" deceleration.");
- }
- }
- momentum += movement;
- momentum = Vector3.ClampMagnitude(momentum, MaxSpeed * SpeedDamper * Time.deltaTime);
- // force player in direction
- //plyr.transform.position = plyr.transform.position + momentum;
- plyr.Move(momentum);
- // Drop player
- ApplyGravity();
- }
- public void SetSlow(float speed) {
- SpeedDamper = speed;
- }
- public void SetRotationLock(bool toggle) {
- }
- void SnapCam() {
- Vector3 camDestination = CollidePoint(transform.position, GetCamPos());
- Camera.main.transform.position = camDestination;
- Camera.main.transform.LookAt(transform.position);
- }
- Vector3 GetCamPos() {
- Vector3 tmp = Vector3.zero;
- float t = (float)(CamDist * Mathf.Cos(ToRadian(pitch)));
- tmp.y = (float)(CamDist * Mathf.Sin(ToRadian(pitch)) + transform.position.y);
- tmp.x = (float)(t * Mathf.Sin(ToRadian(yaw)) + transform.position.x);
- //tmp.Y = (float)this.pos.Y;
- tmp.z = (float)(t * Mathf.Cos(ToRadian(yaw)) + transform.position.z);
- return tmp;
- }
- float ToRadian(float angle)
- {
- return 3.14f * angle / 180.0f;
- }
- // Collision detection for camera on closest non-player object
- Vector3 CollidePoint(Vector3 begin, Vector3 target) {
- RaycastHit[] hits;
- hits = Physics.RaycastAll(begin, (target-begin), (target-begin).magnitude);
- Vector3 closest = target;
- foreach (RaycastHit hit in hits) {
- Vector3 loc = (hit.point + (hit.normal * 0.01f));
- if (CamCollisionTags.Contains(hit.collider.transform.tag))
- if ((begin-loc).magnitude < (begin-closest).magnitude)
- closest = loc;
- }
- return closest;
- }
- // Collision detection for camera on closest non-player object
- Vector3 GetGroundPos() {
- Vector3 begin = plyr.transform.position;
- RaycastHit[] hits;
- hits = Physics.RaycastAll(begin, Vector3.down);
- Vector3 closest = Vector3.zero;
- foreach (RaycastHit hit in hits) {
- Vector3 loc = (hit.point + (hit.normal * 0.1f));
- if (hit.collider.transform.tag == "Environment")
- if ((begin-loc).magnitude < (begin-closest).magnitude || closest == Vector3.zero)
- closest = loc;
- }
- return closest;
- }
- void ApplyGravity() {
- // constant gravitational force
- verticalForce += Gravity * Time.deltaTime;
- // apply normal gravity
- plyr.Move((new Vector3(0, verticalForce, 0)*Time.deltaTime));
- if (plyr.isGrounded) {
- // Animation plays when we hit the ground with a force > 2
- if (Mathf.Abs(verticalForce) >= 2.0f) {
- anim.SetBool("Bounce", true);
- // invert animation speed just a little to accomodate for heavier drops
- anim.speed = 3.0f-Mathf.Clamp(Mathf.Abs(verticalForce), 0.0f, 2.2f);
- } else {
- anim.SetBool("Bounce", false);
- }
- // reset force to prevent reanimations
- verticalForce = 0;
- } else {
- anim.SetBool("Bounce", false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment