Skip to content

Instantly share code, notes, and snippets.

@KABBOUCHI
Forked from IRCSS/SmoothGameCameraMovement.cs
Created January 21, 2020 20:25
Show Gist options
  • Select an option

  • Save KABBOUCHI/987d146f9fb7fe88dc53d79df586b3f1 to your computer and use it in GitHub Desktop.

Select an option

Save KABBOUCHI/987d146f9fb7fe88dc53d79df586b3f1 to your computer and use it in GitHub Desktop.

Revisions

  1. @IRCSS IRCSS renamed this gist Jan 15, 2020. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions MoveCameraNonVR.cs → SmoothGameCameraMovement.cs
    Original file line number Diff line number Diff line change
    @@ -5,24 +5,24 @@

    // Moves Camera similar to how camera is moved in the Unity view port. Drop the scrip on the game object which has the camera and you wish to move.

    public class MoveCameraNonVR : MonoBehaviour
    public class SmoothGameCameraMovement : MonoBehaviour
    {


    public float lateralSpeed = 0.0015f;
    public float LongitudinalSpeed = 0.0008f;
    public float verticalSpeed = 0.0015f;
    public float mouseMoevementSpeed = 0.8f;
    public float verticalSpeed = 0.001f;
    public float mouseMoevementSpeed = 0.2f;
    public float ShiftMultiplyer = 4f;
    public bool InvertedY = false;

    // ------------------------------------------------------

    // these need to be > 0 and <= 1. closer to 0 is smoother movement
    public float verticalFilter = 0.005f;
    public float verticalFilter = 0.003f;
    public float lateralFilter = 0.005f;
    public float LongitudinalFilter = 0.005f;
    public float mouseFilter = 0.1f;
    public float mouseFilter = 0.04f;

    // ------------------------------------------------------

    @@ -48,12 +48,12 @@ void Update()
    {

    // -------------------------------------------------------------------------------------------------------
    if (!Input.GetKey(KeyCode.Mouse1)) return;


    // ========================================
    // Speed Multiplyer
    speedMultiplyer = Mathf.Lerp(speedMultiplyer, 1f, 0.1f);
    if (Input.GetKey(KeyCode.LeftShift))
    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.Mouse1))
    {
    speedMultiplyer += 0.2f;
    speedMultiplyer = Mathf.Clamp(speedMultiplyer, 1f, ShiftMultiplyer);
    @@ -68,7 +68,8 @@ void Update()
    lateral = Mathf.Clamp(lateral, -1f, 1f);

    float x = Input.GetAxis("Horizontal");
    lateral += x * lateralSpeed;

    if (Input.GetKey(KeyCode.Mouse1)) lateral += x * lateralSpeed;

    this.transform.position += this.transform.right * lateral * speedMultiplyer;

    @@ -80,16 +81,16 @@ void Update()

    float y = Input.GetAxis("Vertical");

    longitudinal += y * LongitudinalSpeed;
    if (Input.GetKey(KeyCode.Mouse1)) longitudinal += y * LongitudinalSpeed;
    this.transform.position += this.transform.forward * longitudinal* speedMultiplyer;

    // ========================================
    // Up Down Movement

    vertical = Mathf.Lerp(vertical, 0f, verticalFilter);

    if (Input.GetKey(KeyCode.E)) vertical += verticalSpeed; // UP
    if (Input.GetKey(KeyCode.Q)) vertical -= verticalSpeed; // DOWN
    if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.Mouse1)) vertical += verticalSpeed; // UP
    if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.Mouse1)) vertical -= verticalSpeed; // DOWN


    this.transform.position += this.transform.up * vertical * speedMultiplyer;
    @@ -98,14 +99,14 @@ void Update()
    // Mouse Movement X

    float mouseX = Input.GetAxis("Mouse X");
    TransformDummy.transform.Rotate(Vector3.up * mouseX* mouseMoevementSpeed, Space.World);
    if (Input.GetKey(KeyCode.Mouse1)) TransformDummy.transform.Rotate(Vector3.up * mouseX* mouseMoevementSpeed, Space.World);


    // ========================================
    // Mouse Movement Y

    float mouseY = Input.GetAxis("Mouse Y");
    TransformDummy.transform.Rotate(TransformDummy.transform.right * -1f * mouseY* mouseMoevementSpeed, Space.World);
    if (Input.GetKey(KeyCode.Mouse1)) TransformDummy.transform.Rotate(TransformDummy.transform.right * -1f * mouseY* mouseMoevementSpeed, Space.World);


    this.transform.rotation = Quaternion.Slerp(this.transform.rotation, TransformDummy.transform.rotation, mouseFilter);
  2. @IRCSS IRCSS created this gist Jan 15, 2020.
    115 changes: 115 additions & 0 deletions MoveCameraNonVR.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    // Moves Camera similar to how camera is moved in the Unity view port. Drop the scrip on the game object which has the camera and you wish to move.

    public class MoveCameraNonVR : MonoBehaviour
    {


    public float lateralSpeed = 0.0015f;
    public float LongitudinalSpeed = 0.0008f;
    public float verticalSpeed = 0.0015f;
    public float mouseMoevementSpeed = 0.8f;
    public float ShiftMultiplyer = 4f;
    public bool InvertedY = false;

    // ------------------------------------------------------

    // these need to be > 0 and <= 1. closer to 0 is smoother movement
    public float verticalFilter = 0.005f;
    public float lateralFilter = 0.005f;
    public float LongitudinalFilter = 0.005f;
    public float mouseFilter = 0.1f;

    // ------------------------------------------------------

    private float vertical;
    private float lateral;
    private float longitudinal;
    private float speedMultiplyer = 1f;
    private float inversion = -1f;


    private GameObject TransformDummy;

    void Start()
    {
    if (InvertedY) inversion = 1f;
    else inversion = -1f;

    if (TransformDummy == null) TransformDummy = new GameObject("TransformDummy");
    TransformDummy.transform.rotation= this.transform.rotation;
    }

    void Update()
    {

    // -------------------------------------------------------------------------------------------------------
    if (!Input.GetKey(KeyCode.Mouse1)) return;

    // ========================================
    // Speed Multiplyer
    speedMultiplyer = Mathf.Lerp(speedMultiplyer, 1f, 0.1f);
    if (Input.GetKey(KeyCode.LeftShift))
    {
    speedMultiplyer += 0.2f;
    speedMultiplyer = Mathf.Clamp(speedMultiplyer, 1f, ShiftMultiplyer);
    }



    // ========================================
    // Left Right Movement

    lateral = Mathf.Lerp(lateral, 0f, lateralFilter);
    lateral = Mathf.Clamp(lateral, -1f, 1f);

    float x = Input.GetAxis("Horizontal");
    lateral += x * lateralSpeed;

    this.transform.position += this.transform.right * lateral * speedMultiplyer;

    // ========================================
    // Foward Backward Movement

    longitudinal = Mathf.Lerp(longitudinal, 0f, LongitudinalFilter);
    longitudinal = Mathf.Clamp(longitudinal, -1f, 1f);

    float y = Input.GetAxis("Vertical");

    longitudinal += y * LongitudinalSpeed;
    this.transform.position += this.transform.forward * longitudinal* speedMultiplyer;

    // ========================================
    // Up Down Movement

    vertical = Mathf.Lerp(vertical, 0f, verticalFilter);

    if (Input.GetKey(KeyCode.E)) vertical += verticalSpeed; // UP
    if (Input.GetKey(KeyCode.Q)) vertical -= verticalSpeed; // DOWN


    this.transform.position += this.transform.up * vertical * speedMultiplyer;

    // ========================================
    // Mouse Movement X

    float mouseX = Input.GetAxis("Mouse X");
    TransformDummy.transform.Rotate(Vector3.up * mouseX* mouseMoevementSpeed, Space.World);


    // ========================================
    // Mouse Movement Y

    float mouseY = Input.GetAxis("Mouse Y");
    TransformDummy.transform.Rotate(TransformDummy.transform.right * -1f * mouseY* mouseMoevementSpeed, Space.World);


    this.transform.rotation = Quaternion.Slerp(this.transform.rotation, TransformDummy.transform.rotation, mouseFilter);


    }
    }