2026-04-29 10:13:09 +07:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public Joystick joystick; // Kéo Fixed Joystick ở Canvas vào đây
|
|
|
|
|
public float moveSpeed = 2f;
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2026-05-02 20:23:43 +07:00
|
|
|
if (joystick == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Joystick is not assigned in PlayerController!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 10:13:09 +07:00
|
|
|
// Lấy input từ Joystick
|
|
|
|
|
float horizontal = joystick.Horizontal;
|
|
|
|
|
float vertical = joystick.Vertical;
|
|
|
|
|
|
|
|
|
|
// Di chuyển object theo trục X và Z
|
|
|
|
|
Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;
|
|
|
|
|
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
|
|
|
|
|
|
|
|
|
|
// (Tùy chọn) Xoay object theo hướng di chuyển
|
|
|
|
|
if (direction != Vector3.zero)
|
|
|
|
|
{
|
|
|
|
|
transform.forward = direction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|