using System.
Collections;
using [Link];
using UnityEngine;
using TMPro;
public class PlayerMovementTutorial : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[HideInInspector] public float walkSpeed;
[HideInInspector] public float sprintSpeed;
[Header("Keybinds")]
public KeyCode jumpKey = [Link];
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
[Link] = true;
readyToJump = true;
}
private void Update()
{
// ground check
grounded = [Link]([Link], [Link], playerHeight *
0.5f + 0.3f, whatIsGround);
MyInput();
SpeedControl();
// handle drag
if (grounded)
[Link] = groundDrag;
else
[Link] = 0;
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
horizontalInput = [Link]("Horizontal");
verticalInput = [Link]("Vertical");
// when to jump
if([Link](jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void MovePlayer()
{
// calculate movement direction
moveDirection = [Link] * verticalInput + [Link] *
horizontalInput;
// on ground
if(grounded)
[Link]([Link] * moveSpeed * 10f,
[Link]);
// in air
else if(!grounded)
[Link]([Link] * moveSpeed * 10f * airMultiplier,
[Link]);
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3([Link].x, 0f, [Link].z);
// limit velocity if needed
if([Link] > moveSpeed)
{
Vector3 limitedVel = [Link] * moveSpeed;
[Link] = new Vector3(limitedVel.x, [Link].y, limitedVel.z);
}
}
private void Jump()
{
// reset y velocity
[Link] = new Vector3([Link].x, 0f, [Link].z);
[Link]([Link] * jumpForce, [Link]);
}
private void ResetJump()
{
readyToJump = true;
}
}