MiniGames/Assets/Scripts/BlockDrop/BlockDrop_PlayerController.cs

81 lines
2.0 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
public class BlockDrop_PlayerController : MonoBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 5f;
public float jumpForce = 7f;
[Header("References")]
public Joystick joystick; // drag joystick here (UI Joystick)
public Transform groundCheck;
public LayerMask groundLayer;
private Rigidbody rb;
private Animator animator;
private float moveInput;
private bool isGrounded;
[Header("Ground Check Settings")]
public float checkRadius = 0.3f;
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void Update()
{
// Joystick input
moveInput = joystick.Horizontal;
// Rotate player to face direction
if (moveInput > 0.1f)
transform.rotation = Quaternion.Euler(0, 90, 0); // face right
else if (moveInput < -0.1f)
transform.rotation = Quaternion.Euler(0, -90, 0); // face left
// Run animation (Speed param)
//if (moveInput !=0)
{
animator.SetFloat("Speed", Mathf.Abs(moveInput));
//animator.SetFloat("Speed", Mathf.Clamp(Mathf.Abs(moveInput), 0.8f, 1f));
}
//else
//{
// animator.SetFloat("Speed", 0);
//}
if (Input.GetButtonDown("Jump"))
{
JumpButton();
}
animator.SetBool("IsGrounded", isGrounded);
}
void FixedUpdate()
{
// Move player left/right
Vector3 velocity = rb.velocity;
velocity.x = moveInput * moveSpeed;
rb.velocity = velocity;
// Ground check (Physics.OverlapSphere)
isGrounded = Physics.CheckSphere(groundCheck.position, checkRadius, groundLayer);
}
// Called by Jump Button
public void JumpButton()
{
if (isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
animator.SetTrigger("Jump");
}
}
}