feat: player can look around on all axis
This commit is contained in:
37
Assets/Scripts/CameraController.cs
Normal file
37
Assets/Scripts/CameraController.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
private InputMaster _inputMaster;
|
||||
[SerializeField] private float mouseSensitivity = 20f;
|
||||
[SerializeField] private Transform playerBody;
|
||||
|
||||
private float _mouseX, _mouseY;
|
||||
private float xRotation;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_inputMaster = new InputMaster();
|
||||
_inputMaster.Player.Look.Enable();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var mousevalue = _inputMaster.Player.Look.ReadValue<Vector2>();
|
||||
_mouseX = mousevalue.x * mouseSensitivity * Time.deltaTime;
|
||||
_mouseY = mousevalue.y * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
xRotation -= _mouseY;
|
||||
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
|
||||
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
||||
|
||||
playerBody.Rotate(Vector3.up * _mouseX);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CameraController.cs.meta
Normal file
11
Assets/Scripts/CameraController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 884f7e9cec877e7bb994f6c21263233f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -8,10 +8,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
private InputMaster _inputMaster;
|
||||
[SerializeField] private CharacterController controller;
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] private float mouseSensitivity = 20f;
|
||||
[SerializeField] private Transform playerBody;
|
||||
|
||||
private float _mouseX, _mouseY;
|
||||
|
||||
private Vector2 _moveDirection;
|
||||
|
||||
@@ -19,18 +15,9 @@ public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
_inputMaster = new InputMaster();
|
||||
_inputMaster.Player.Look.Enable();
|
||||
_inputMaster.Player.Move.Enable();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var mousevalue = _inputMaster.Player.Look.ReadValue<Vector2>();
|
||||
_mouseX = mousevalue.x * mouseSensitivity * Time.deltaTime;
|
||||
_mouseY = mousevalue.y * mouseSensitivity * Time.deltaTime;
|
||||
playerBody.Rotate(Vector3.up * _mouseX);
|
||||
}
|
||||
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
_moveDirection = context.ReadValue<Vector2>();
|
||||
|
||||
Reference in New Issue
Block a user