using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody rb;
Animator animator;
bool isJumping = false;
[SerializeField] float movementSpeed = 6f;
[SerializeField] float jumpForce = 5f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Calculate the movement direction based on input
Vector3 movementDirection = new Vector3(horizontalInput, 0f, verticalInput).normalized;
if (movementDirection != Vector3.zero)
{
// Calculate the rotation angle based on the movement direction
float targetAngle = Mathf.Atan2(movementDirection.x, movementDirection.z) * Mathf.Rad2Deg;
// Rotate the player to face the target angle
transform.rotation = Quaternion.Euler(0, targetAngle, 0);
// Move the player in the specified direction
rb.velocity = new Vector3(movementDirection.x * movementSpeed, rb.velocity.y, movementDirection.z * movementSpeed);
// Set the "IsWalking" parameter in the animator to true
animator.SetBool("IsWalking", true);
}
else
{
// Set the "IsWalking" parameter in the animator to false
animator.SetBool("IsWalking", false);
}
if (Input.GetButtonDown("Jump") && !isJumping)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
// Set the "IsJumping" parameter in the animator to true
animator.SetBool("IsJumping", true);
isJumping = true;
}
else
{
// Set the "IsJumping" parameter in the animator to false
animator.SetBool("IsJumping", false);
isJumping = false;
}
}
}