forked from cgvr/DeltaVR
npcs turn smoothly towards player
This commit is contained in:
@@ -5,17 +5,15 @@ using UnityEngine;
|
||||
|
||||
public abstract class NPCController : MonoBehaviour
|
||||
{
|
||||
protected Transform playerTransform;
|
||||
[Header("Movement Config")]
|
||||
public Transform mouth;
|
||||
public float turnSpeed = 5f;
|
||||
|
||||
[Header("Voiceline Amplitude Timeline Config")]
|
||||
public string voicelinesFolder = "CharacterVoicelines";
|
||||
public string characterSpecificFolder;
|
||||
public string[] voiceLineKeys;
|
||||
|
||||
|
||||
[Header("Mouth Transform")]
|
||||
public Transform mouth; // assign your billboard mouth object
|
||||
|
||||
[Header("Mouth Animation Settings")]
|
||||
public float minScaleY = 0.3f;
|
||||
public float maxScaleY = 1.0f;
|
||||
@@ -24,6 +22,8 @@ public abstract class NPCController : MonoBehaviour
|
||||
public float release = 0.2f; // slower closing
|
||||
public bool inverted = false;
|
||||
|
||||
protected Transform playerTransform;
|
||||
|
||||
private float[] rmsCurve;
|
||||
private EventInstance currentVoicelineEvent;
|
||||
private bool isSpeaking;
|
||||
@@ -50,9 +50,19 @@ public abstract class NPCController : MonoBehaviour
|
||||
{
|
||||
if (playerTransform != null)
|
||||
{
|
||||
// As if player is on same Y coordinate as this object, to not rotate around Y axis.
|
||||
// Keep target on same Y as this object (no pitch/roll), so we only yaw.
|
||||
Vector3 lookTargetPos = new Vector3(playerTransform.position.x, transform.position.y, playerTransform.position.z);
|
||||
transform.LookAt(lookTargetPos);
|
||||
|
||||
// Direction from this object to the target (on horizontal plane)
|
||||
Vector3 toTarget = (lookTargetPos - transform.position);
|
||||
if (toTarget.sqrMagnitude > 0.0001f) // avoid zero-length
|
||||
{
|
||||
Quaternion targetRot = Quaternion.LookRotation(toTarget.normalized);
|
||||
|
||||
// Interpolate a little each frame towards the target rotation.
|
||||
float t = Mathf.Clamp01(Time.deltaTime * turnSpeed);
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, t);
|
||||
}
|
||||
}
|
||||
|
||||
if (isSpeaking && rmsCurve != null && currentVoicelineEvent.isValid())
|
||||
|
||||
Reference in New Issue
Block a user