Created
November 7, 2021 16:25
-
-
Save MrSimsek/33ef478ef01b912041a6980a117161b2 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 PlayerController : MonoBehaviour | |
{ | |
private CharacterController _controller; | |
[SerializeField] | |
private float _playerSpeed = 5f; | |
[SerializeField] | |
private float _rotationSpeed = 10f; | |
[SerializeField] | |
private Camera _followCamera; | |
private Vector3 _playerVelocity; | |
private bool _groundedPlayer; | |
[SerializeField] | |
private float _jumpHeight = 1.0f; | |
[SerializeField] | |
private float _gravityValue = -9.81f; | |
private void Start() | |
{ | |
_controller = GetComponent<CharacterController>(); | |
} | |
private void Update() | |
{ | |
Movement(); | |
} | |
void Movement() | |
{ | |
_groundedPlayer = _controller.isGrounded; | |
if (_groundedPlayer && _playerVelocity.y < 0) | |
{ | |
_playerVelocity.y = 0f; | |
} | |
float horizontalInput = Input.GetAxis("Horizontal"); | |
float verticalInput = Input.GetAxis("Vertical"); | |
Vector3 movementInput = Quaternion.Euler(0, _followCamera.transform.eulerAngles.y, 0) * new Vector3(horizontalInput, 0, verticalInput); | |
Vector3 movementDirection = movementInput.normalized; | |
_controller.Move(movementDirection * _playerSpeed * Time.deltaTime); | |
if (movementDirection != Vector3.zero) | |
{ | |
Quaternion desiredRotation = Quaternion.LookRotation(movementDirection, Vector3.up); | |
transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, _rotationSpeed * Time.deltaTime); | |
} | |
if (Input.GetButtonDown("Jump") && _groundedPlayer) | |
{ | |
_playerVelocity.y += Mathf.Sqrt(_jumpHeight * -3.0f * _gravityValue); | |
} | |
_playerVelocity.y += _gravityValue * Time.deltaTime; | |
_controller.Move(_playerVelocity * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment