using UnityEditor.Rendering;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public int Speed = 10;
public int RunSpeed = 5;
public int TargetFrameRate = 10;
public float JumpForce = 100;
public bool CanJump = false;
private Animator playerAnimator;
private bool Run = false;
private Rigidbody playerRigidBody;
private void Start()
{
playerAnimator = GetComponentInChildren<Animator>();
playerRigidBody = GetComponent<Rigidbody>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Ground")
{
CanJump = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Ground")
{
CanJump = false;
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Ground")
{
CanJump = true;
}
}
void Update()
{
Application.targetFrameRate = TargetFrameRate;
// Remove Input system using package manager
float horizontal = Input.GetAxis("Horizontal"); // For Right or Left Input
// datatype name assigns RHS(=) Class.Method(Parameter)
float vertical = Input.GetAxis("Vertical"); // For Forward or Backward
Run = Input.GetKey(KeyCode.LeftShift);
if (Input.GetKeyDown(KeyCode.Space))
{
if (CanJump == true)
playerRigidBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
playerAnimator.SetTrigger("CanJump");
playerAnimator.SetBool("Ground", CanJump);
}
Vector3 direction = horizontal * Vector3.right
+ vertical * Vector3.forward;
playerAnimator.SetFloat("Speed", Mathf.Abs(horizontal) + Mathf.Abs(vertical));
playerAnimator.SetBool("Run", Run);
if (direction.magnitude > 0)
{
if (Run == true)
{
transform.position += direction * RunSpeed * Time.deltaTime;
}
else
{
transform.position += direction * Speed * Time.deltaTime;
}
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction, Vector3.up), 0.25f);
}
}
}4 views