1
0
forked from cgvr/DeltaVR

make sprite NPC look at player

This commit is contained in:
2026-01-06 18:43:59 +02:00
parent 79dcf3bc9b
commit e4d61a6648
4 changed files with 169 additions and 146 deletions

View File

@@ -1,13 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Unity.XR.CoreUtils;
using UnityEngine;
public class NPCController : MonoBehaviour
{
private Vector3 mouthClosedScale;
private Vector3 mouthOpenScale;
private bool isTalking;
private Transform playerTransform;
public Transform mouthTransform;
public float mouthScalingMultiplier = 2.5f;
@@ -25,7 +25,40 @@ public class NPCController : MonoBehaviour
// Update is called once per frame
void Update()
{
if (playerTransform != null)
{
// As if player is on same Y coordinate as this object, to not rotate around Y axis.
Vector3 lookTargetPos = new Vector3(playerTransform.position.x, transform.position.y, playerTransform.position.z);
transform.LookAt(lookTargetPos);
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Collided with " + other);
KbmController controller = other.GetComponent<KbmController>();
XROrigin playerOrigin = other.GetComponent<XROrigin>();
if (controller != null)
{
playerTransform = controller.transform;
} else if (playerOrigin != null)
{
playerTransform = playerOrigin.transform;
}
}
private void OnTriggerExit(Collider other)
{
KbmController controller = other.GetComponent<KbmController>();
XROrigin playerOrigin = other.GetComponent<XROrigin>();
if (controller != null)
{
playerTransform = null;
}
else if (playerOrigin != null)
{
playerTransform = null;
}
}
public void StartTalking()