24 lines
1.0 KiB
C#
24 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
public class ARFirstPersonController : MonoBehaviour
|
|
{
|
|
public Transform player; // Kéo nhân vật vào đây
|
|
public Transform imageTarget; // Kéo ImageTarget vào đây
|
|
public Vector3 viewOffset = new Vector3(0, -1.6f, 0.5f); // Điều chỉnh để mắt nhân vật khớp với camera
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (player == null || imageTarget == null) return;
|
|
|
|
// Ép vị trí của toàn bộ ImageTarget di chuyển sao cho Player luôn nằm ở vị trí Camera
|
|
// Điều này tạo ra góc nhìn thứ nhất trong AR
|
|
Vector3 targetPosition = Camera.main.transform.position + Camera.main.transform.TransformDirection(viewOffset);
|
|
|
|
// Di chuyển ImageTarget để bù trừ vị trí
|
|
imageTarget.position = targetPosition;
|
|
|
|
// Xoay ImageTarget để hướng nhìn của nhân vật khớp với hướng điện thoại
|
|
imageTarget.rotation = Quaternion.LookRotation(Camera.main.transform.forward);
|
|
}
|
|
}
|