using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
[SerializeField] private Transform PlayerCamera;
[SerializeField] private CharacterController PlayerController;
[SerializeField] private float MouseSensitivity = 5f;
[SerializeField] private float MovementSpeed = 4f;
[SerializeField] private float JumpForce = 12f;
[SerializeField] private float Gravity = -9.81f;
private Vector3 Velocity;
private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;
// Update is called once per frame
void Update()
{
PlayerMovementInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
Move();
Look();
}
void Move()
{
Vector3 MoveVector = transform.forward * PlayerMovementInput.z + transform.right * PlayerMovementInput.x;
if(PlayerController.isGrounded)
{
Velocity.y = 3f;
if(Input.GetKeyDown(KeyCode.Space))
{
Velocity.y = JumpForce;
}
}
else
{
Velocity.y -= Gravity * -2f * Time.deltaTime;
}
PlayerController.Move(MoveVector * MovementSpeed * Time.deltaTime);
PlayerController.Move(Velocity * Time.deltaTime);
}
void Look()
{
CamXRotation -= PlayerMouseInput.y * MouseSensitivity;
CamXRotation = Mathf.Clamp(CamXRotation, -90f, 90f);
transform.Rotate(0f, PlayerMouseInput.x, 0f);
PlayerCamera.localRotation = Quaternion.Euler(CamXRotation 0f, 0f);
}