PlayerMovementTutorial Cs
PlayerMovementTutorial Cs
Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
readyToJump = true;
}
MyInput();
SpeedControl();
// handle drag
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;
}
// when to jump
if(Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
// on ground
if(grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f,
ForceMode.Force);
// in air
else if(!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier,
ForceMode.Force);
}