MariuszPoz

Creating a flycamera in unity

Jan 7th, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.InputSystem;
  4.  
  5. public class FlyCamera : MonoBehaviour
  6. {
  7.  
  8.     /*
  9.     Writen by Windexglow 11-13-10.  Use it, edit it, steal it I don't care.  
  10.     Converted to C# 27-02-13 - no credit wanted.
  11.     Extended by Mahelita 08-01-18.
  12.         Added up and down movement using e and q, respectively.
  13.         Adjusted parameters to fit my needs.
  14.     Updated to the new Unity input sytem by @bison_42 / bison-- 2022-01-02
  15.     Simple flycam I made, since I couldn't find any others made public.  
  16.     Made simple to use (drag and drop, done) for regular keyboard layout  
  17.     wasd : basic movement
  18.     qe : Move camera down or up, respectively
  19.     shift : Makes camera accelerate
  20.     space : Moves camera on X and Z axis only.  So camera doesn't gain any height
  21.     */
  22.  
  23.  
  24.     public float mainSpeed = 5.0f; //regular speed
  25.     public float shiftAdd = 25f; //multiplied by how long shift is held.  Basically running
  26.     public float maxShift = 100.0f; //Maximum speed when holdin gshift
  27.     public float camSens = 0.25f; //How sensitive it with mouse
  28.     public bool rotateOnlyIfMousedown = true;
  29.     public bool movementStaysFlat = false;
  30.  
  31.     private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
  32.     private float totalRun = 1.0f;
  33.  
  34.     void Update()
  35.     {
  36.  
  37.         if (Mouse.current.rightButton.wasPressedThisFrame)
  38.         {
  39.             lastMouse = new Vector3(Mouse.current.position.ReadValue().x, Mouse.current.position.ReadValue().y, 0); // $CTK reset when we begin
  40.         }
  41.  
  42.         if (!rotateOnlyIfMousedown ||
  43.             (rotateOnlyIfMousedown && Mouse.current.rightButton.IsPressed()))
  44.         {
  45.             lastMouse = new Vector3(Mouse.current.position.ReadValue().x, Mouse.current.position.ReadValue().y, 0) - lastMouse;
  46.             lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
  47.             lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
  48.             transform.eulerAngles = lastMouse;
  49.             lastMouse = Mouse.current.position.ReadValue();
  50.             //Mouse  camera angle done.  
  51.         }
  52.  
  53.         //Keyboard commands
  54.         float f = 0.0f;
  55.         Vector3 p = GetBaseInput();
  56.         if (Keyboard.current.leftShiftKey.IsPressed())
  57.         {
  58.             totalRun += Time.deltaTime;
  59.             p = p * totalRun * shiftAdd;
  60.             p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  61.             p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  62.             p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  63.         }
  64.         else
  65.         {
  66.             totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  67.             p = p * mainSpeed;
  68.         }
  69.  
  70.         p = p * Time.deltaTime;
  71.         Vector3 newPosition = transform.position;
  72.         if (Keyboard.current.spaceKey.IsPressed()
  73.             || (movementStaysFlat && !(rotateOnlyIfMousedown && Mouse.current.rightButton.IsPressed())))
  74.         { //If player wants to move on X and Z axis only
  75.             transform.Translate(p);
  76.             newPosition.x = transform.position.x;
  77.             newPosition.z = transform.position.z;
  78.             transform.position = newPosition;
  79.         }
  80.         else
  81.         {
  82.             transform.Translate(p);
  83.         }
  84.  
  85.     }
  86.  
  87.     private Vector3 GetBaseInput()
  88.     { //returns the basic values, if it's 0 than it's not active.
  89.         Vector3 p_Velocity = new Vector3();
  90.         if (Keyboard.current.wKey.IsPressed())
  91.         {
  92.             p_Velocity += new Vector3(0, 0, 1);
  93.         }
  94.         if (Keyboard.current.sKey.IsPressed())
  95.         {
  96.             p_Velocity += new Vector3(0, 0, -1);
  97.         }
  98.         if (Keyboard.current.aKey.IsPressed())
  99.         {
  100.             p_Velocity += new Vector3(-1, 0, 0);
  101.         }
  102.         if (Keyboard.current.dKey.IsPressed())
  103.         {
  104.             p_Velocity += new Vector3(1, 0, 0);
  105.         }
  106.         if (Keyboard.current.qKey.IsPressed())
  107.         {
  108.             p_Velocity += new Vector3(0, -1, 0);
  109.         }
  110.         if (Keyboard.current.eKey.IsPressed())
  111.         {
  112.             p_Velocity += new Vector3(0, 1, 0);
  113.         }
  114.         return p_Velocity;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment