Skip to content

Instantly share code, notes, and snippets.

@MrSimsek
Created November 7, 2021 16:25
Show Gist options
  • Save MrSimsek/33ef478ef01b912041a6980a117161b2 to your computer and use it in GitHub Desktop.
Save MrSimsek/33ef478ef01b912041a6980a117161b2 to your computer and use it in GitHub Desktop.
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