Facebook
From Sexy Dolphin, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 147
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NewBehaviourScript : MonoBehaviour {
  5.  
  6.     // Use this for initialization
  7.     void OnPreRender()
  8.     {
  9.         GL.wireframe = true;
  10.     }
  11.     void OnPostRender()
  12.     {
  13.         GL.wireframe = false;
  14.     }
  15.  
  16.     public float mouseSensitivity = 5.0f;        // Mouse rotation sensitivity.
  17.     public float speed = 10.0f;    // Regular speed.
  18.     public float gravity = 20.0f;    // Gravity force.
  19.     public float shiftAdd = 25.0f;    // Multiplied by how long shift is held.  Basically running.
  20.     public float maxShift = 100.0f;    // Maximum speed when holding shift.
  21.     public bool walkerMode = false;    // Walker Mode.
  22.  
  23.     private float totalRun = 1.0f;
  24.     private float rotationY = 0.0f;
  25.     private float maximumY = 90.0f;    // Not recommended to change
  26.     private float minimumY = -90.0f;    // these parameters.
  27.     private CharacterController controller;
  28.  
  29.     void Start()
  30.     {
  31.         controller = GetComponent<CharacterController>();
  32.         Cursor.lockState = CursorLockMode.Locked;
  33.     }
  34.  
  35.     void Update()
  36.     {
  37.         if (Input.GetKeyUp(KeyCode.Q))
  38.         {
  39.             // Toggle mode.
  40.             walkerMode = !walkerMode;
  41.         }
  42.     }
  43.  
  44.     void FixedUpdate()
  45.     {
  46.         // Mouse commands.
  47.         float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSensitivity;
  48.         rotationY += Input.GetAxis("Mouse Y") * mouseSensitivity;
  49.         rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  50.         transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0.0f);
  51.  
  52.         // Keyboard commands.
  53.         Vector3 p = getDirection();
  54.         if (Input.GetKey(KeyCode.LeftShift))
  55.         {
  56.             totalRun += Time.deltaTime;
  57.             p = p * totalRun * shiftAdd;
  58.             p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  59.             p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  60.             p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  61.         }
  62.         else
  63.         {
  64.             totalRun = Mathf.Clamp(totalRun * 0.5f, 1.0f, 1000.0f);
  65.             p = p * speed;
  66.         }
  67.  
  68.         p = p * Time.deltaTime;
  69.         Vector3 newPosition = transform.position;
  70.         if (walkerMode)
  71.         {
  72.             // Walker Mode.
  73.             p = transform.TransformDirection(p);
  74.             p.y = 0.0f;
  75.             p.y -= gravity * Time.deltaTime;
  76.             controller.Move(p);
  77.         }
  78.         else
  79.         {
  80.             // Fly Mode.
  81.             if (Input.GetButton("Jump"))
  82.             { // If player wants to move on X and Z axis only (sliding)
  83.                 transform.Translate(p);
  84.                 newPosition.x = transform.position.x;
  85.                 newPosition.z = transform.position.z;
  86.                 transform.position = newPosition;
  87.             }
  88.             else
  89.             {
  90.                 transform.Translate(p);
  91.             }
  92.         }
  93.     }
  94.  
  95.     private Vector3 getDirection()
  96.     {
  97.         Vector3 p_Velocity = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
  98.         // Strifing enabled only in Fly Mode.
  99.         if (!walkerMode)
  100.         {
  101.             if (Input.GetKey(KeyCode.F))
  102.             {
  103.                 p_Velocity += new Vector3(0.0f, -1.0f, 0.0f);
  104.             }
  105.             if (Input.GetKey(KeyCode.R))
  106.             {
  107.                 p_Velocity += new Vector3(0.0f, 1.0f, 0.0f);
  108.             }
  109.         }
  110.         return p_Velocity;
  111.     }
  112.  
  113.     public void resetRotation(Vector3 lookAt)
  114.     {
  115.         transform.LookAt(lookAt);
  116.     }
  117.  
  118. }
  119.