Advertisement
Guest User

FlyCamera.cs

a guest
Sep 14th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FlyCamera : MonoBehaviour
  5. {
  6.     /*
  7.     Writen by Windexglow 11-13-10.  Use it, edit it, steal it I don't care.
  8.     Converted to C# 27-02-13 - no credit wanted.
  9.     Reformatted and cleaned by Ryan Breaker 23-6-18
  10.  
  11.     Original comment:
  12.     Simple flycam I made, since I couldn't find any others made public.
  13.     Made simple to use (drag and drop, done) for regular keyboard layout.
  14.  
  15.     Controls:
  16.     WASD  : Directional movement
  17.     Shift : Increase speed
  18.     Space : Moves camera directly up per its local Y-axis
  19.     */
  20.  
  21.     public float mainSpeed = 10.0f;   // Regular speed
  22.     public float shiftAdd  = 25.0f;   // Amount to accelerate when shift is pressed
  23.     public float maxShift  = 100.0f;  // Maximum speed when holding shift
  24.     public float camSens   = 0.15f;   // Mouse sensitivity
  25.  
  26.     private Vector3 lastMouse = new Vector3(255, 255, 255);  // kind of in the middle of the screen, rather than at the top (play)
  27.     private float totalRun = 1.0f;
  28.  
  29.     void Update()
  30.     {
  31.         //lastMouse = Input.mousePosition - lastMouse;
  32.         //lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
  33.         //lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
  34.         //transform.eulerAngles = lastMouse;
  35.         //lastMouse = Input.mousePosition;
  36.         // Mouse camera angle done.  
  37.  
  38.         // Keyboard commands
  39.         Vector3 p = GetBaseInput();
  40.         if (Input.GetKey(KeyCode.LeftShift))
  41.         {
  42.             totalRun += Time.deltaTime;
  43.             p *= totalRun * shiftAdd;
  44.             p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  45.             p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  46.             p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  47.         }
  48.         else
  49.         {
  50.             totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  51.             p *= mainSpeed;
  52.         }
  53.  
  54.         p *= Time.deltaTime;
  55.         transform.Translate(p);
  56.     }
  57.  
  58.     // Returns the basic values, if it's 0 than it's not active.
  59.     private Vector3 GetBaseInput()
  60.     {
  61.         Vector3 p_Velocity = new Vector3();
  62.  
  63.         // Forwards
  64.         if (Input.GetKey(KeyCode.W))
  65.             p_Velocity += new Vector3(0, 0, 1);
  66.  
  67.         // Backwards
  68.         if (Input.GetKey(KeyCode.S))
  69.             p_Velocity += new Vector3(0, 0, -1);
  70.  
  71.         // Left
  72.         if (Input.GetKey(KeyCode.A))
  73.             p_Velocity += new Vector3(-1, 0, 0);
  74.  
  75.         // Right
  76.         if (Input.GetKey(KeyCode.D))
  77.             p_Velocity += new Vector3(1, 0, 0);
  78.  
  79.         // Up
  80.         if (Input.GetKey(KeyCode.Space))
  81.             p_Velocity += new Vector3(0, 1, 0);
  82.  
  83.         // Down
  84.         if (Input.GetKey(KeyCode.LeftControl))
  85.             p_Velocity += new Vector3(0, -1, 0);
  86.  
  87.         return p_Velocity;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement