GameVsJam/3d Prototyp/Assets/Animations/NPCAnimationController.cs

203 lines
7.4 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 dancePlace;
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);
StartCoroutine(GetToWorkRoutine());
}
private IEnumerator GetToWorkRoutine()
{
animator.SetLayerWeight(animator.GetLayerIndex("Typing"), 0);
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
MoveTo(workPosition.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zurück
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
transform.rotation = workPosition.rotation;
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
}
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()
{
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 0, 1f));
// 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ü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ück zum Ausgangspunkt.
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
animator.ResetTrigger("SitDown"); // Setze den Sitzen-Trigger zurück
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
MoveTo(workPosition.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zurück
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
transform.rotation = workPosition.rotation;
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
}
// Hier kannst du entscheiden, ob der Charakter wieder sitzt oder steht.
// Beispiel: Setze IsSitting oder IsStanding entsprechend.
public void GettingMad()
{
StartCoroutine(GettingMadRoutine());
}
private IEnumerator GettingMadRoutine()
{
animator.SetTrigger("GetMad");
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 1, 2f)); // 1 Sekunde zum Einblenden
yield return new WaitForSeconds(8f);
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 0, 1f)); // 1 Sekunde zum Ausblenden
animator.ResetTrigger("GetMad");
}
public void CaffeinOverdose()
{
StartCoroutine(CaffeinRoutine());
}
private IEnumerator CaffeinRoutine()
{
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 0, 1f));
// Beginne mit dem Laufen zur Toilette.
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Walk"));
MoveTo(dancePlace.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk");
animator.SetTrigger("TooMuchCaffein");
yield return new WaitForSeconds(8f);
animator.ResetTrigger("TooMuchCaffein");
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ück
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
}
private void MoveTo(Vector3 destination)
{
agent.destination = destination;
}
private void Update()
{
// Prüft, ob der Buchstabe 'C' gedrückt wurde
if (Input.GetKeyDown(KeyCode.C))
{
DrinkCoffee();
}
// Prüft, ob der Buchstabe 'T' gedrückt wurde
if (Input.GetKeyDown(KeyCode.T))
{
GoToToilet();
}
// Prüft, ob der Buchstabe 'M' gedrückt wurde
if (Input.GetKeyDown(KeyCode.M))
{
GettingMad();
}
// Prüft, ob der Buchstabe 'C' gedrückt wurde
if (Input.GetKeyDown(KeyCode.O))
{
CaffeinOverdose();
}
}
}