1
0
forked from cgvr/DeltaVR
Files
DeltaVR3DModelGeneration/Assets/_PROJECT/Scripts/NPCController.cs

95 lines
2.4 KiB
C#

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;
public float mouthMovementDuration = 0.25f;
// Start is called before the first frame update
void Awake()
{
mouthClosedScale = mouthTransform.localScale;
mouthOpenScale = new Vector3(mouthClosedScale.x, mouthClosedScale.y * mouthScalingMultiplier, mouthClosedScale.z);
isTalking = false;
}
// 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()
{
isTalking = true;
MoveMouth();
}
public void Stoptalking()
{
isTalking = false;
}
private void MoveMouth()
{
if (!isTalking)
{
return;
}
if (mouthTransform.localScale == mouthClosedScale)
{
mouthTransform.DOScale(mouthOpenScale, mouthMovementDuration);
}
else
{
mouthTransform.DOScale(mouthClosedScale, mouthMovementDuration);
}
Invoke("MoveMouth", mouthMovementDuration + 0.01f);
}
}