148 lines
3.9 KiB
C#
148 lines
3.9 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class CubeClash_ZibuController : MonoBehaviour
|
|
{
|
|
public static List<CubeClash_ZibuController> AllZibus = new List<CubeClash_ZibuController>();
|
|
|
|
[Header("Control")]
|
|
public bool isPlayerControlled = true;
|
|
|
|
[Header("Movement Settings")]
|
|
public float moveSpeed = 5f;
|
|
public Joystick joystick;
|
|
|
|
[Header("Bump Settings")]
|
|
public float bumpDistance = 3f;
|
|
public float bumpSpeed = 20f;
|
|
public float returnSpeed = 10f;
|
|
public float cooldown = 2f;
|
|
public float knockbackForce = 15f;
|
|
|
|
[Header("Animation")]
|
|
public Animator animator; // assign in Inspector
|
|
|
|
private Rigidbody rb;
|
|
private bool canBump = true;
|
|
[HideInInspector] public bool isBumping = false;
|
|
|
|
void OnEnable()
|
|
{
|
|
if (!AllZibus.Contains(this))
|
|
AllZibus.Add(this);
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
AllZibus.Remove(this);
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
if (animator == null) animator = GetComponentInChildren<Animator>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isPlayerControlled && !isBumping)
|
|
{
|
|
HandleMovement();
|
|
}
|
|
|
|
UpdateAnimations();
|
|
}
|
|
|
|
private void HandleMovement()
|
|
{
|
|
if (joystick != null)
|
|
{
|
|
Vector3 move = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
|
|
if (move.magnitude > 0.1f)
|
|
{
|
|
Vector3 targetVelocity = move.normalized * moveSpeed;
|
|
rb.velocity = new Vector3(targetVelocity.x, rb.velocity.y, targetVelocity.z);
|
|
|
|
// face only in Y-axis
|
|
Quaternion targetRot = Quaternion.LookRotation(new Vector3(move.x, 0, move.z));
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * 10f);
|
|
}
|
|
else
|
|
{
|
|
rb.velocity = new Vector3(0, rb.velocity.y, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateAnimations()
|
|
{
|
|
if (animator != null)
|
|
{
|
|
// Use velocity magnitude (XZ only) to set "Speed"
|
|
Vector3 horizontalVel = new Vector3(rb.velocity.x, 0, rb.velocity.z);
|
|
float speed = horizontalVel.magnitude;
|
|
|
|
animator.SetFloat("Speed", speed);
|
|
}
|
|
}
|
|
|
|
public void PerformBump()
|
|
{
|
|
if (!canBump || isBumping) return;
|
|
StartCoroutine(BumpRoutine(transform.forward));
|
|
}
|
|
|
|
public void PerformBump(Vector3 direction)
|
|
{
|
|
if (!canBump || isBumping) return;
|
|
StartCoroutine(BumpRoutine(direction));
|
|
}
|
|
|
|
private IEnumerator BumpRoutine(Vector3 direction)
|
|
{
|
|
canBump = false;
|
|
isBumping = true;
|
|
|
|
Vector3 startPos = transform.position;
|
|
Vector3 targetPos = startPos + direction.normalized * bumpDistance;
|
|
|
|
float t = 0f;
|
|
while (t < 1f)
|
|
{
|
|
t += Time.deltaTime * bumpSpeed;
|
|
rb.MovePosition(Vector3.Lerp(startPos, targetPos, t));
|
|
yield return null;
|
|
}
|
|
|
|
t = 0f;
|
|
while (t < 1f)
|
|
{
|
|
t += Time.deltaTime * returnSpeed;
|
|
rb.MovePosition(Vector3.Lerp(targetPos, startPos, t));
|
|
yield return null;
|
|
}
|
|
|
|
isBumping = false;
|
|
|
|
yield return new WaitForSeconds(cooldown);
|
|
canBump = true;
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
CubeClash_ZibuController other = collision.gameObject.GetComponent<CubeClash_ZibuController>();
|
|
if (other != null)
|
|
{
|
|
if (isBumping && other.isBumping) return;
|
|
|
|
if (isBumping && !other.isBumping && other.rb != null)
|
|
{
|
|
Vector3 pushDir = transform.forward.normalized;
|
|
other.rb.AddForce(pushDir * knockbackForce, ForceMode.Impulse);
|
|
}
|
|
}
|
|
}
|
|
}
|