Files
VR-GAME/Assets/Script/PlayerController.cs

41 lines
1.3 KiB
C#
Raw Normal View History

using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Joystick joystick; // Kéo Fixed Joystick ở Canvas vào đây
public float moveSpeed = 2f;
2026-05-04 14:52:40 +07:00
public Animator animator; // Kéo Animator của nhân vật vào đây
void Update()
{
2026-05-02 20:23:43 +07:00
if (joystick == null)
{
Debug.LogWarning("Joystick is not assigned in PlayerController!");
return;
}
// 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;
2026-05-04 14:52:40 +07:00
float currentSpeed = direction.magnitude * moveSpeed;
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
2026-05-04 14:52:40 +07:00
// Xoay object mượt mà theo hướng di chuyển
if (direction != Vector3.zero)
{
2026-05-04 14:52:40 +07:00
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 15f * Time.deltaTime);
}
// Cập nhật Animator
if (animator != null)
{
// Giả sử Animator có parameter "Speed" kiểu Float
animator.SetFloat("Speed", currentSpeed);
}
}
}