Search Unity

Toggling Between Different Speeds On A Free Cam Script

Discussion in 'Scripting' started by jason07, Apr 14, 2019.

  1. jason07

    jason07

    Joined:
    May 10, 2011
    Posts:
    34
    Hi,

    I found a script for a free cam that has the ability to hold the left shift key to move at a faster speed. I would like to toggle the faster speed so you don't have to hold down the shift key, but am having some trouble getting it to work. I've tried adding a boolean variable and using a key press to turn that on and off and reassign the fast movement speed to the regular movement speed but that doesn't seem to work even though it is toggling the bool on and off. Does anyone have any suggestions on what I might be overlooking? Thanks!

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6.  
    7. public class FreeCam : MonoBehaviour
    8. {
    9.     public float movementSpeed = 10f;
    10.     public float fastMovementSpeed = 100f;
    11.     public float freeLookSensitivity = 3f;
    12.     public float zoomSensitivity = 10f;
    13.     public float fastZoomSensitivity = 50f;
    14.     private bool looking = false;
    15.     private bool slowSpeed= true;
    16.  
    17.     void Update()
    18.     {
    19.         var fastMode = Input.GetKey(KeyCode.LeftShift)
    20.         var movementSpeed = fastMode ? this.fastMovementSpeed : this.movementSpeed;
    21.         if(Input.GetButton("FastSpeedToggle"))  
    22.         {
    23.             if(slowSpeed)
    24.             {
    25.                 movementSpeed = fastMovementSpeed;
    26.                 slowSpeed= false;
    27.              }
    28.             else
    29.             {
    30.                 movementSpeed =10f;
    31.                 slowSpeed= true;
    32.              }
    33.         }
    34.  
    35.         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
    36.         {
    37.             transform.position = transform.position + (-transform.right * movementSpeed * Time.deltaTime);
    38.         }
    39.  
    40.         if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
    41.         {
    42.             transform.position = transform.position + (transform.right * movementSpeed * Time.deltaTime);
    43.         }
    44.  
    45.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
    46.         {
    47.             transform.position = transform.position + (transform.forward * movementSpeed * Time.deltaTime);
    48.         }
    49.  
    50.         if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
    51.         {
    52.             transform.position = transform.position + (-transform.forward * movementSpeed * Time.deltaTime);
    53.         }
    54.  
    55.         if (Input.GetKey(KeyCode.Q))
    56.         {
    57.             transform.position = transform.position + (transform.up * movementSpeed * Time.deltaTime);
    58.         }
    59.  
    60.         if (Input.GetKey(KeyCode.E))
    61.         {
    62.             transform.position = transform.position + (-transform.up * movementSpeed * Time.deltaTime);
    63.         }
    64.  
    65.         if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.PageUp))
    66.         {
    67.             transform.position = transform.position + (Vector3.up * movementSpeed * Time.deltaTime);
    68.         }
    69.  
    70.         if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.PageDown))
    71.         {
    72.             transform.position = transform.position + (-Vector3.up * movementSpeed * Time.deltaTime);
    73.         }
    74.  
    75.         if (looking)
    76.         {
    77.             float newRotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * freeLookSensitivity;
    78.             float newRotationY = transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * freeLookSensitivity;
    79.             transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f);
    80.         }
    81.  
    82.         float axis = Input.GetAxis("Mouse ScrollWheel");
    83.         if (axis != 0)
    84.         {
    85.             var zoomSensitivity = fastMode ? this.fastZoomSensitivity : this.zoomSensitivity;
    86.             transform.position = transform.position + transform.forward * axis * zoomSensitivity;
    87.         }
    88.  
    89.         if (Input.GetKeyDown(KeyCode.Mouse1))
    90.         {
    91.             StartLooking();
    92.         }
    93.         else if (Input.GetKeyUp(KeyCode.Mouse1))
    94.         {
    95.             StopLooking();
    96.         }
    97.     }
    98.  
    99.     void OnDisable()
    100.     {
    101.         StopLooking();
    102.     }
    103.  
    104.     public void StartLooking()
    105.     {
    106.         looking = true;
    107.         Cursor.visible = false;
    108.         Cursor.lockState = CursorLockMode.Locked;
    109.     }
    110.  
    111.     public void StopLooking()
    112.     {
    113.         looking = false;
    114.         Cursor.visible = true;
    115.         Cursor.lockState = CursorLockMode.None;
    116.     }
    117. }
     
  2. Kretoskar

    Kretoskar

    Joined:
    Mar 15, 2019
    Posts:
    11
    Try using Input.GetKeyDown instead of Input.GetKey.
     
  3. jason07

    jason07

    Joined:
    May 10, 2011
    Posts:
    34
    Thanks, Kretoskar. In the end, I actually ended up using Playmaker to make the switch between the two values.
     
    Kretoskar likes this.