MiniGames/Assets/Scripts/BlockDrop/BlockDrop_WolfController3D.cs

173 lines
5.3 KiB
C#

using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class BlockDrop_WolfController3D : MonoBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 6f;
public float jumpForce = 9f;
public float maxHorizontalSpeed = 10f;
[Header("Jump Settings")]
public float coyoteTime = 0.12f;
public float jumpBuffer = 0.12f;
[Header("References")]
public Joystick joystick; // UI joystick
public Transform groundCheck; // ground check point
public LayerMask groundLayer; // ground mask
public Animator animator;
[Header("Ground Check Settings")]
public float groundCheckRadius = 0.2f;
private Rigidbody rb;
private float lastGroundedTime;
private float lastJumpPressedTime;
private bool jumpQueued;
private bool isGrounded;
void Awake()
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
}
void Update()
{
// Joystick input
float moveInput = joystick != null ? joystick.Horizontal : Input.GetAxisRaw("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
// Animator speed
if (animator != null)
{
animator.SetFloat("Speed", Mathf.Abs(moveInput));
animator.SetBool("IsGrounded", isGrounded);
}
// Jump input from keyboard/controller
if (Input.GetButtonDown("Jump"))
{
lastJumpPressedTime = Time.time;
jumpQueued = true;
}
}
void FixedUpdate()
{
// Ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
if (isGrounded) lastGroundedTime = Time.time;
// Joystick movement
float moveInput = joystick != null ? joystick.Horizontal : Input.GetAxisRaw("Horizontal");
Vector3 vel = rb.velocity;
vel.x = moveInput * moveSpeed;
vel.x = Mathf.Clamp(vel.x, -maxHorizontalSpeed, maxHorizontalSpeed);
rb.velocity = new Vector3(vel.x, vel.y, 0f);
// Handle jump with coyote time + jump buffer
if (jumpQueued && (Time.time - lastJumpPressedTime) <= jumpBuffer && (Time.time - lastGroundedTime) <= coyoteTime)
{
Vector3 v = rb.velocity; v.y = 0f; rb.velocity = v;
rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
if (animator != null) animator.SetTrigger("Jump");
jumpQueued = false;
}
// Reset queued jump if grounded
if (isGrounded) jumpQueued = false;
}
// Called by UI Jump Button
public void JumpButton()
{
lastJumpPressedTime = Time.time;
jumpQueued = true;
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bunny") || other.CompareTag("CarrotTrap"))
{
BlockDrop_GameManager3D.Instance.GameOver("Wolf caught or exploded!");
}
}
}
//using UnityEngine;
//[RequireComponent(typeof(Rigidbody))]
//public class BlockDrop_WolfController3D : MonoBehaviour
//{
// [Header("Movement")]
// public float moveSpeed = 6f;
// public float jumpForce = 9f;
// public float coyoteTime = 0.12f;
// public float jumpBuffer = 0.12f;
// public float maxHorizontalSpeed = 10f;
// public float planeZ = 0f;
// public LayerMask groundLayer;
// public Transform groundCheck;
// public float groundCheckRadius = 0.2f;
// private Rigidbody rb;
// private float lastGroundedTime;
// private float lastJumpPressedTime;
// private bool jumpQueued;
// void Awake()
// {
// rb = GetComponent<Rigidbody>();
// rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
// Vector3 p = transform.position; p.z = planeZ; transform.position = p;
// }
// void Update()
// {
// if (Input.GetButtonDown("Jump"))
// {
// lastJumpPressedTime = Time.time;
// jumpQueued = true;
// }
// }
// void FixedUpdate()
// {
// bool grounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
// if (grounded) lastGroundedTime = Time.time;
// float x = Input.GetAxisRaw("Horizontal");
// Vector3 vel = rb.velocity;
// vel.x = x * moveSpeed;
// vel.z = 0f;
// vel.x = Mathf.Clamp(vel.x, -maxHorizontalSpeed, maxHorizontalSpeed);
// rb.velocity = new Vector3(vel.x, vel.y, 0f);
// if (jumpQueued && (Time.time - lastJumpPressedTime) <= jumpBuffer && (Time.time - lastGroundedTime) <= coyoteTime)
// {
// Vector3 v = rb.velocity; v.y = 0f; rb.velocity = v;
// rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
// }
// if (grounded) jumpQueued = false;
// }
// void OnTriggerEnter(Collider other)
// {
// if (other.CompareTag("Bunny") || other.CompareTag("CarrotTrap"))
// {
// BlockDrop_GameManager3D.Instance.GameOver("Wolf caught or exploded!");
// }
// }
//}