Skip to content

Instantly share code, notes, and snippets.

@xCyborg
Last active October 17, 2019 00:44
Show Gist options
  • Save xCyborg/39d8e95a42790e31ede790c0d5d7ad15 to your computer and use it in GitHub Desktop.
Save xCyborg/39d8e95a42790e31ede790c0d5d7ad15 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraManouver : MonoBehaviour
{
Transform _transform; // cache transform component of the camera
[Header("Keyboard Translation")]
public float horizontalFactor = 10f;
public float verticalFactor = 10f;
[Header("Mouse Camera Look")]
public float mouseSensitivity = 100.0f;
[Tooltip("Range of motion around the X axis, up and down between [-value, value]")]
public float clampAngle = 80.0f;
private float rotY = 0.0f; // rotation around the up/y axis == Left vs Right (sideways)
private float rotX = 0.0f; // rotation around the right/x axis == Up vs Down (Vertically)
// Start is called before the first frame update
void Start()
{
_transform = GetComponent<Transform>();
Vector3 rot = _transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
// Update is called once per frame
void Update()
{
// Keyboard Translation ==================
var haxis = Input.GetAxis("Horizontal");
var vaxis = Input.GetAxis("Vertical");
_transform.Translate(haxis * Vector3.right * horizontalFactor * Time.deltaTime, Space.Self);
_transform.Translate(vaxis * Vector3.forward * verticalFactor * Time.deltaTime, Space.Self);
// Mouse Free Look ========================
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
//:::::::::
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment