Komak57

pc.cs

Jun 22nd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.     public enum ControlType {
  6.         Accelerometer,
  7.         Gyroscope,
  8.         Controller
  9.     }
  10.     InputManager.KeySettings input = null;
  11.     public ControlType InputMethod = ControlType.Accelerometer;
  12.     Vector3 AimDir = Vector3.zero;
  13.     Vector3 LookZero = Vector3.zero;
  14.     Vector3 LookDir {
  15.         get {
  16.             switch (InputMethod) {
  17.             case ControlType.Accelerometer:
  18.                 return new Vector3(Input.acceleration.x, Input.acceleration.y*180f, Input.acceleration.z*90f) - LookZero;
  19.             case ControlType.Gyroscope:
  20.                 return (new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0)).eulerAngles - LookZero;
  21.             default:
  22.                 return new Vector3(input.Right.getValue()-input.Left.getValue(), input.Down.getValue()-input.Up.getValue(), 0);
  23.             }
  24.         }
  25.     }
  26.     public float AimSensitivity = 100f;
  27.     public float MoveSpeed = 10f;
  28.     public float DeadZone = 0.01f;
  29.     CharacterController cc;
  30.     public Transform cam;
  31.     public Transform eyes;
  32.     Vector3 last = Vector3.zero;
  33.     Gyroscope gyro;
  34.     // Use this for initialization
  35.     void Start () {
  36.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
  37.         cc = GetComponent<CharacterController> ();
  38.         if (cam == null)
  39.             cam = transform.FindChild ("Cam");
  40.         if (eyes == null)
  41.             eyes = cam.FindChild ("Eyes");
  42.         input = new InputManager.KeySettings ();//InputManager.KeySettings.Load();
  43.         Debug.Log ("Look Key: " + input.LookDown.Axis);
  44.         //Input.compensateSensors = true;
  45.  
  46.         if (InputMethod == ControlType.Gyroscope) {
  47.             // Fallback for unsupported devices
  48.             if (!SystemInfo.supportsGyroscope) {
  49.                 InputMethod = ControlType.Accelerometer;
  50.             } else {
  51.                 gyro = Input.gyro;
  52.                 gyro.enabled = true;
  53.                 gyro.updateInterval = 0.0167f; //60hz
  54.             }
  55.         }
  56.         LookZero = LookDir;
  57.         last = LookDir;
  58.     }
  59.     void Unload() {
  60.         Screen.sleepTimeout = SleepTimeout.SystemSetting;
  61.     }
  62.     void OnDrawGizmos() {
  63.         Gizmos.color = Color.red;
  64.         if (eyes != null) {
  65.             Vector3 to = eyes.position + eyes.forward * 10;
  66.             Gizmos.DrawLine (eyes.position, to);
  67.             Gizmos.DrawLine (new Vector3(to.x, to.y-5, to.z), new Vector3(to.x, to.y+5, to.z));
  68.             Gizmos.DrawLine (new Vector3(to.x-5, to.y, to.z), new Vector3(to.x+5, to.y, to.z));
  69.         }
  70.  
  71.         Gizmos.color = Color.blue;
  72.         if (cam != null) {
  73.             Vector3 to = cam.position + cam.forward * 12;
  74.             Gizmos.DrawLine (cam.position, to);
  75.             Gizmos.DrawLine (new Vector3(to.x, to.y-6, to.z), new Vector3(to.x, to.y+6, to.z));
  76.             Gizmos.DrawLine (new Vector3(to.x-6, to.y, to.z), new Vector3(to.x+6, to.y, to.z));
  77.         }
  78.  
  79.     }
  80.     // Update is called once per frame
  81.     void Update () {
  82.         //Vector3 dir = new Vector3(input.MoveRight.getValue() - input.MoveLeft.getValue(), 0, input.MoveForward.getValue() - input.MoveBack.getValue()) * MoveSpeed;
  83.         // actual direction
  84.         AimDir += new Vector3 (input.LookRight.getValue () - input.LookLeft.getValue (), input.LookUp.getValue () - input.LookDown.getValue ())*Time.deltaTime*AimSensitivity;
  85.         //cam.rotation = Quaternion.Euler(new Vector3(Input.acceleration.z, 0, Input.acceleration.x));
  86.         cam.rotation = Quaternion.Euler(new Vector3(AimDir.y, AimDir.x, 0));
  87.         cc.Move(MoveDirection()*Time.deltaTime);
  88.  
  89.         // variant direction
  90.         switch (InputMethod) {
  91.         case ControlType.Accelerometer:
  92.             if (LookDir.sqrMagnitude >= DeadZone)
  93.                 eyes.Rotate(new Vector3(last.z-LookDir.z, 0, 0));
  94.             if (LookDir.sqrMagnitude >= DeadZone)
  95.                 eyes.Rotate (new Vector3 (0, LookDir.y-last.y+AimDir.x, 0));
  96.             if (LookDir.sqrMagnitude >= DeadZone)
  97.                 last = LookDir;
  98.             break;
  99.         case ControlType.Gyroscope:
  100.             eyes.Rotate(new Vector3(LookDir.x-last.x+AimDir.y, 0, 0));
  101.             eyes.Rotate (new Vector3 (0, LookDir.y-last.y+AimDir.x, 0));
  102.             last = LookDir;
  103.             break;
  104.         default:
  105.             float vert = (input.Up.isHeld()? 1:0)-(input.Down.isHeld()? 1:0);
  106.             float horiz = (input.Right.isHeld()? 1:0)-(input.Left.isHeld()? 1:0);
  107.             Vector3 _input = new Vector3(vert, (horiz*0.7f), 0);
  108.             //eyes.LookAt(movement);
  109.             eyes.Rotate(_input);
  110.             //eyes.Rotate(new Vector3(LookDir.y, 0, 0));
  111.             //eyes.Rotate (new Vector3 (0, input.LookRight.getValue () - input.LookLeft.getValue (), 0));
  112.             break;
  113.         }
  114.     }
  115.     Vector3 MoveDirection() {
  116.         // Get desired direction with accelleration modifiers
  117.         //Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
  118.         float vert = (input.MoveForward.isHeld()? 1:0)-(input.MoveBack.isHeld()? 1:0);
  119.         float horiz = (input.MoveRight.isHeld()? 1:0)-(input.MoveLeft.isHeld()? 1:0);
  120.         Vector3 _input = new Vector3(vert, 0, (horiz*0.7f));
  121.         // Direction Translators
  122.         Vector3 forward = cam.forward;//Camera.main.transform.TransformDirection(Vector3.forward);
  123.         forward.y = 0; forward = forward.normalized;
  124.         Vector3 right = new Vector3(forward.z, 0, -forward.x);
  125.         // calculate horizontal acceleration
  126.         Vector3 movement = (_input.z * right + _input.x * forward);
  127.        
  128.         movement = movement * MoveSpeed;
  129.         // clamp only when too high
  130.         if (movement.magnitude > MoveSpeed)
  131.             movement = Vector3.ClampMagnitude(movement, MoveSpeed);
  132.         return movement;
  133.     }
  134.     Vector3 ForwardNormalize(float vert, float horiz, Vector3 forward) {
  135.         // Get desired direction with accelleration modifiers
  136.         //Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
  137.         //float vert = (input.MoveForward.isHeld()? 1:0)-(input.MoveBack.isHeld()? 1:0);
  138.         //float horiz = (input.MoveRight.isHeld()? 1:0)-(input.MoveLeft.isHeld()? 1:0);
  139.         Vector3 _input = new Vector3(vert, 0, (horiz*0.7f));
  140.         // Direction Translators
  141.         //Vector3 forward = cam.forward;//Camera.main.transform.TransformDirection(Vector3.forward);
  142.         forward.y = 0; forward = forward.normalized;
  143.         Vector3 right = new Vector3(forward.z, 0, -forward.x);
  144.         // calculate horizontal acceleration
  145.         Vector3 movement = (_input.z * right + _input.x * forward);
  146.        
  147.         movement = movement * MoveSpeed;
  148.         // clamp only when too high
  149.         if (movement.magnitude > MoveSpeed)
  150.             movement = Vector3.ClampMagnitude(movement, MoveSpeed);
  151.         return movement;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment