Skip to content

Instantly share code, notes, and snippets.

@vbayeva
Created November 8, 2024 21:30
Show Gist options
  • Select an option

  • Save vbayeva/60a285cf964d49b02e3c677b4ce75275 to your computer and use it in GitHub Desktop.

Select an option

Save vbayeva/60a285cf964d49b02e3c677b4ce75275 to your computer and use it in GitHub Desktop.
Player movement script using New Input System. Movement, dash, acceleration, gravity
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
[SerializeField] private float maxSpeed = 5f;
[SerializeField] private float rotationSpeed = 360f;
[SerializeField] private float accelerationFactor = 5f;
[SerializeField] private float decelerationFactor = 10f;
[SerializeField] private float gravity = -9.81f;
[Header("Dash")]
[SerializeField] private float dashingCooldown = 1.5f;
[SerializeField] private float dashingTime = 0.2f;
[SerializeField] private float dashingSpeed = 8f;
private bool _canDash;
private bool _isDashing;
private bool _dashInput;
private Vector3 _velocity;
private float _currentSpeed;
private InputSystem_Actions _playerInputActions;
private Vector3 _input;
private CharacterController _characterController;
private bool _isGrounded;
private void Awake()
{
_playerInputActions = new InputSystem_Actions();
_characterController = GetComponent<CharacterController>();
}
private void OnEnable()
{
_playerInputActions.Player.Enable();
_canDash = true;
}
private void OnDisable()
{
_playerInputActions.Player.Disable();
}
private void Update()
{
_isGrounded = _characterController.isGrounded;
if (_isGrounded && _velocity.y < 0)
{
_velocity.y = -2f;
}
if (!_isGrounded)
{
_velocity.y += gravity * Time.fixedDeltaTime;
}
GatherInput();
Look();
CalculareSpeed();
Move();
if (_dashInput && _canDash)
{
StartCoroutine(Dash());
}
}
private IEnumerator Dash()
{
_canDash = false;
_isDashing = true;
yield return new WaitForSeconds(dashingTime);
_isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
_canDash = true;
}
private void CalculareSpeed()
{
if (_input == Vector3.zero && _currentSpeed > 0)
{
_currentSpeed -= decelerationFactor * Time.deltaTime;
}
else if (_input != Vector3.zero && _currentSpeed < maxSpeed)
{
_currentSpeed += accelerationFactor * Time.deltaTime;
}
_currentSpeed = Mathf.Clamp( _currentSpeed, 0, maxSpeed );
}
private void Look()
{
if (_input == Vector3.zero) return;
Matrix4x4 isometricMatrix = Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));
Vector3 multipliedMatrix = isometricMatrix.MultiplyPoint3x4(_input);
Quaternion rotation = Quaternion.LookRotation(multipliedMatrix, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}
private void Move()
{
if (_isDashing)
{
_characterController.Move(transform.forward * dashingSpeed * Time.deltaTime);
return;
}
Vector3 moveDirection = transform.forward * _currentSpeed * Time.deltaTime + _velocity;
_characterController.Move(moveDirection);
}
private void GatherInput()
{
Vector2 input = _playerInputActions.Player.Move.ReadValue<Vector2>();
_input = new Vector3(input.x, 0, input.y);
_dashInput = _playerInputActions.Player.Sprint.IsPressed();
Debug.Log(_input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment