Last active
August 15, 2021 18:57
-
-
Save MrSimsek/70a361472a30e93fec74910773115bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CameraControl : MonoBehaviour { | |
[SerializeField] | |
private GameObject player; | |
[SerializeField] | |
private float _sensitivityX = 1f, _sensitivityY = 1f; | |
void Update() | |
{ | |
HandleLookX(); | |
HandleLookY(); | |
} | |
private void HandleLookX() | |
{ | |
float mouseX = Input.GetAxis("Mouse X"); | |
Vector3 rotation = player.transform.localEulerAngles; | |
rotation.y += mouseX * _sensitivityX; | |
player.transform.localEulerAngles = rotation; | |
} | |
private void HandleLookY() | |
{ | |
float mouseY = Input.GetAxis("Mouse Y"); | |
Vector3 rotation = transform.localEulerAngles; | |
rotation.x += mouseY * _sensitivityY; | |
rotation.x = (rotation.x > 180) ? rotation.x - 360 : rotation.x; | |
rotation.x = Mathf.Clamp(rotation.x, -70, 60); | |
transform.localEulerAngles = rotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment