44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(CharacterController))]
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
[Header("Movement")]
|
|
public float moveSpeed = 6f;
|
|
public float gravity = -20f;
|
|
|
|
private CharacterController characterController;
|
|
private Vector3 verticalVelocity;
|
|
|
|
private void Awake()
|
|
{
|
|
characterController = GetComponent<CharacterController>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
float horizontal = Input.GetAxisRaw("Horizontal");
|
|
float vertical = Input.GetAxisRaw("Vertical");
|
|
|
|
Vector3 input = new Vector3(horizontal, 0f, vertical);
|
|
input = Vector3.ClampMagnitude(input, 1f);
|
|
|
|
Vector3 move = input * moveSpeed;
|
|
|
|
if (characterController.isGrounded && verticalVelocity.y < 0f)
|
|
{
|
|
verticalVelocity.y = -1f;
|
|
}
|
|
|
|
verticalVelocity.y += gravity * Time.deltaTime;
|
|
move += verticalVelocity;
|
|
|
|
characterController.Move(move * Time.deltaTime);
|
|
|
|
if (input.sqrMagnitude > 0.001f)
|
|
{
|
|
transform.rotation = Quaternion.LookRotation(input, Vector3.up);
|
|
}
|
|
}
|
|
}
|