142 lines
4.6 KiB
C#
142 lines
4.6 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEngine.AI;
|
|||
|
using System.Collections;
|
|||
|
|
|||
|
public class NPCAnimationController : MonoBehaviour
|
|||
|
{
|
|||
|
public Transform workPosition;
|
|||
|
|
|||
|
public GameObject Cup;
|
|||
|
public GameObject HandCup;
|
|||
|
|
|||
|
|
|||
|
public Transform toiletTransform;
|
|||
|
public Transform toiletLookAtTransform;// Die Transform-Position der Toilette
|
|||
|
private NavMeshAgent agent;
|
|||
|
private Animator animator;
|
|||
|
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
agent = GetComponent<NavMeshAgent>();
|
|||
|
animator = GetComponent<Animator>();
|
|||
|
HandCup.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private IEnumerator FadeLayerWeight(int layerIndex, float targetWeight, float duration)
|
|||
|
{
|
|||
|
float time = 0;
|
|||
|
float startWeight = animator.GetLayerWeight(layerIndex);
|
|||
|
|
|||
|
while (time < duration)
|
|||
|
{
|
|||
|
animator.SetLayerWeight(layerIndex, Mathf.Lerp(startWeight, targetWeight, time / duration));
|
|||
|
time += Time.deltaTime;
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
|
|||
|
animator.SetLayerWeight(layerIndex, targetWeight); // Sicherstellen, dass der Zielwert erreicht wird
|
|||
|
}
|
|||
|
|
|||
|
public void DrinkCoffee()
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
StartCoroutine(DrinkCoffeeRoutine());
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator DrinkCoffeeRoutine()
|
|||
|
{
|
|||
|
|
|||
|
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 1, 1f)); // 1 Sekunde zum Einblenden
|
|||
|
animator.SetTrigger("DrinkCoffee");
|
|||
|
yield return new WaitForSeconds(1.5f);
|
|||
|
Cup.SetActive(false);
|
|||
|
yield return new WaitForSeconds(1.5f);
|
|||
|
HandCup.SetActive(true);
|
|||
|
yield return new WaitForSeconds(3.5f);
|
|||
|
HandCup.SetActive(false);
|
|||
|
|
|||
|
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 0, 1f)); // 1 Sekunde zum Ausblenden
|
|||
|
animator.ResetTrigger("DrinkCoffee");
|
|||
|
Cup.SetActive(true);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void GoToToilet()
|
|||
|
{
|
|||
|
StartCoroutine(GoToToiletRoutine());
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator GoToToiletRoutine()
|
|||
|
{
|
|||
|
animator.SetLayerWeight(animator.GetLayerIndex("Typing"), 0);
|
|||
|
// Beginne mit dem Laufen zur Toilette.
|
|||
|
animator.SetTrigger("Walk");
|
|||
|
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Walk"));
|
|||
|
MoveTo(toiletTransform.position);
|
|||
|
// Warte, bis der Charakter am Ziel angekommen ist.
|
|||
|
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
|
|||
|
|
|||
|
|
|||
|
// H<>rt auf zu laufen und beginnt zu sitzen.
|
|||
|
animator.ResetTrigger("Walk"); // Es ist wichtig, den vorherigen Trigger zur<75>ckzusetzen
|
|||
|
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
|
|||
|
animator.SetTrigger("SitDown");
|
|||
|
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
|
|||
|
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Ready"));
|
|||
|
// Stehe auf und gehe zur<75>ck zum Ausgangspunkt.
|
|||
|
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
|
|||
|
animator.ResetTrigger("SitDown"); // Setze den Sitzen-Trigger zur<75>ck
|
|||
|
animator.SetTrigger("Walk");
|
|||
|
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
|
|||
|
MoveTo(workPosition.position);
|
|||
|
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
|
|||
|
transform.rotation = workPosition.rotation;
|
|||
|
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zur<75>ck
|
|||
|
animator.SetTrigger("SitDown");
|
|||
|
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
|
|||
|
animator.SetLayerWeight(animator.GetLayerIndex("Typing"), 1);
|
|||
|
}
|
|||
|
// Hier kannst du entscheiden, ob der Charakter wieder sitzt oder steht.
|
|||
|
// Beispiel: Setze IsSitting oder IsStanding entsprechend.
|
|||
|
|
|||
|
|
|||
|
public void AskForMoney()
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private void MoveTo(Vector3 destination)
|
|||
|
{
|
|||
|
agent.destination = destination;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
// Pr<50>ft, ob der Buchstabe 'C' gedr<64>ckt wurde
|
|||
|
if (Input.GetKeyDown(KeyCode.C))
|
|||
|
{
|
|||
|
DrinkCoffee();
|
|||
|
}
|
|||
|
|
|||
|
// Pr<50>ft, ob der Buchstabe 'T' gedr<64>ckt wurde
|
|||
|
if (Input.GetKeyDown(KeyCode.T))
|
|||
|
{
|
|||
|
GoToToilet();
|
|||
|
}
|
|||
|
|
|||
|
// Pr<50>ft, ob der Buchstabe 'M' gedr<64>ckt wurde
|
|||
|
if (Input.GetKeyDown(KeyCode.M))
|
|||
|
{
|
|||
|
AskForMoney();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|