Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- public enum ControlType {
- Accelerometer,
- Gyroscope,
- Controller
- }
- InputManager.KeySettings input = null;
- public ControlType InputMethod = ControlType.Accelerometer;
- Vector3 AimDir = Vector3.zero;
- Vector3 LookZero = Vector3.zero;
- Vector3 LookDir {
- get {
- switch (InputMethod) {
- case ControlType.Accelerometer:
- return new Vector3(Input.acceleration.x, Input.acceleration.y*180f, Input.acceleration.z*90f) - LookZero;
- case ControlType.Gyroscope:
- return (new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0)).eulerAngles - LookZero;
- default:
- return new Vector3(input.Right.getValue()-input.Left.getValue(), input.Down.getValue()-input.Up.getValue(), 0);
- }
- }
- }
- public float AimSensitivity = 100f;
- public float MoveSpeed = 10f;
- public float DeadZone = 0.01f;
- CharacterController cc;
- public Transform cam;
- public Transform eyes;
- Vector3 last = Vector3.zero;
- Gyroscope gyro;
- // Use this for initialization
- void Start () {
- Screen.sleepTimeout = SleepTimeout.NeverSleep;
- cc = GetComponent<CharacterController> ();
- if (cam == null)
- cam = transform.FindChild ("Cam");
- if (eyes == null)
- eyes = cam.FindChild ("Eyes");
- input = new InputManager.KeySettings ();//InputManager.KeySettings.Load();
- Debug.Log ("Look Key: " + input.LookDown.Axis);
- //Input.compensateSensors = true;
- if (InputMethod == ControlType.Gyroscope) {
- // Fallback for unsupported devices
- if (!SystemInfo.supportsGyroscope) {
- InputMethod = ControlType.Accelerometer;
- } else {
- gyro = Input.gyro;
- gyro.enabled = true;
- gyro.updateInterval = 0.0167f; //60hz
- }
- }
- LookZero = LookDir;
- last = LookDir;
- }
- void Unload() {
- Screen.sleepTimeout = SleepTimeout.SystemSetting;
- }
- void OnDrawGizmos() {
- Gizmos.color = Color.red;
- if (eyes != null) {
- Vector3 to = eyes.position + eyes.forward * 10;
- Gizmos.DrawLine (eyes.position, to);
- Gizmos.DrawLine (new Vector3(to.x, to.y-5, to.z), new Vector3(to.x, to.y+5, to.z));
- Gizmos.DrawLine (new Vector3(to.x-5, to.y, to.z), new Vector3(to.x+5, to.y, to.z));
- }
- Gizmos.color = Color.blue;
- if (cam != null) {
- Vector3 to = cam.position + cam.forward * 12;
- Gizmos.DrawLine (cam.position, to);
- Gizmos.DrawLine (new Vector3(to.x, to.y-6, to.z), new Vector3(to.x, to.y+6, to.z));
- Gizmos.DrawLine (new Vector3(to.x-6, to.y, to.z), new Vector3(to.x+6, to.y, to.z));
- }
- }
- // Update is called once per frame
- void Update () {
- //Vector3 dir = new Vector3(input.MoveRight.getValue() - input.MoveLeft.getValue(), 0, input.MoveForward.getValue() - input.MoveBack.getValue()) * MoveSpeed;
- // actual direction
- AimDir += new Vector3 (input.LookRight.getValue () - input.LookLeft.getValue (), input.LookUp.getValue () - input.LookDown.getValue ())*Time.deltaTime*AimSensitivity;
- //cam.rotation = Quaternion.Euler(new Vector3(Input.acceleration.z, 0, Input.acceleration.x));
- cam.rotation = Quaternion.Euler(new Vector3(AimDir.y, AimDir.x, 0));
- cc.Move(MoveDirection()*Time.deltaTime);
- // variant direction
- switch (InputMethod) {
- case ControlType.Accelerometer:
- if (LookDir.sqrMagnitude >= DeadZone)
- eyes.Rotate(new Vector3(last.z-LookDir.z, 0, 0));
- if (LookDir.sqrMagnitude >= DeadZone)
- eyes.Rotate (new Vector3 (0, LookDir.y-last.y+AimDir.x, 0));
- if (LookDir.sqrMagnitude >= DeadZone)
- last = LookDir;
- break;
- case ControlType.Gyroscope:
- eyes.Rotate(new Vector3(LookDir.x-last.x+AimDir.y, 0, 0));
- eyes.Rotate (new Vector3 (0, LookDir.y-last.y+AimDir.x, 0));
- last = LookDir;
- break;
- default:
- float vert = (input.Up.isHeld()? 1:0)-(input.Down.isHeld()? 1:0);
- float horiz = (input.Right.isHeld()? 1:0)-(input.Left.isHeld()? 1:0);
- Vector3 _input = new Vector3(vert, (horiz*0.7f), 0);
- //eyes.LookAt(movement);
- eyes.Rotate(_input);
- //eyes.Rotate(new Vector3(LookDir.y, 0, 0));
- //eyes.Rotate (new Vector3 (0, input.LookRight.getValue () - input.LookLeft.getValue (), 0));
- break;
- }
- }
- Vector3 MoveDirection() {
- // Get desired direction with accelleration modifiers
- //Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
- float vert = (input.MoveForward.isHeld()? 1:0)-(input.MoveBack.isHeld()? 1:0);
- float horiz = (input.MoveRight.isHeld()? 1:0)-(input.MoveLeft.isHeld()? 1:0);
- Vector3 _input = new Vector3(vert, 0, (horiz*0.7f));
- // Direction Translators
- Vector3 forward = cam.forward;//Camera.main.transform.TransformDirection(Vector3.forward);
- forward.y = 0; forward = forward.normalized;
- Vector3 right = new Vector3(forward.z, 0, -forward.x);
- // calculate horizontal acceleration
- Vector3 movement = (_input.z * right + _input.x * forward);
- movement = movement * MoveSpeed;
- // clamp only when too high
- if (movement.magnitude > MoveSpeed)
- movement = Vector3.ClampMagnitude(movement, MoveSpeed);
- return movement;
- }
- Vector3 ForwardNormalize(float vert, float horiz, Vector3 forward) {
- // Get desired direction with accelleration modifiers
- //Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
- //float vert = (input.MoveForward.isHeld()? 1:0)-(input.MoveBack.isHeld()? 1:0);
- //float horiz = (input.MoveRight.isHeld()? 1:0)-(input.MoveLeft.isHeld()? 1:0);
- Vector3 _input = new Vector3(vert, 0, (horiz*0.7f));
- // Direction Translators
- //Vector3 forward = cam.forward;//Camera.main.transform.TransformDirection(Vector3.forward);
- forward.y = 0; forward = forward.normalized;
- Vector3 right = new Vector3(forward.z, 0, -forward.x);
- // calculate horizontal acceleration
- Vector3 movement = (_input.z * right + _input.x * forward);
- movement = movement * MoveSpeed;
- // clamp only when too high
- if (movement.magnitude > MoveSpeed)
- movement = Vector3.ClampMagnitude(movement, MoveSpeed);
- return movement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment