Anna Lise Moves To the Spot and takes a picture when the bird is there.
This commit is contained in:
@@ -1,39 +1,53 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
using Pathfinding;
|
||||
|
||||
public class AnneLiseBehaviour : MonoBehaviour
|
||||
{
|
||||
// Movement speed of AnneLise
|
||||
[SerializeField] public float moveSpeed;
|
||||
// Coroutine reference to manage movement
|
||||
private Coroutine moveCoroutine;
|
||||
// Event triggered when AnneLise arrives at the target spot
|
||||
public event Action OnArrivedAtSpot;
|
||||
private Animator animator;
|
||||
private AIPath aiPath;
|
||||
private bool hasArrived = false; // Flag to prevent repeated triggering
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
aiPath = GetComponent<AIPath>();
|
||||
if (aiPath != null)
|
||||
{
|
||||
aiPath.maxSpeed = moveSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
// Method to initiate movement to the specified lure spot
|
||||
public void GotoSpot(GameObject lurespot)
|
||||
{
|
||||
if (moveCoroutine != null)
|
||||
StopCoroutine(moveCoroutine);
|
||||
if (aiPath == null) return;
|
||||
|
||||
moveCoroutine = StartCoroutine(MoveToPosition(lurespot.transform.position));
|
||||
aiPath.destination = lurespot.transform.position;
|
||||
aiPath.canMove = true;
|
||||
aiPath.SearchPath();
|
||||
hasArrived = false; // Reset flag when a new destination is set
|
||||
}
|
||||
// Coroutine to move AnneLise to the target position
|
||||
private IEnumerator MoveToPosition(Vector3 targetPosition)
|
||||
|
||||
private void Update()
|
||||
{
|
||||
while (Vector3.Distance(transform.position, targetPosition) > 0.05f)
|
||||
if (aiPath == null || animator == null) return;
|
||||
|
||||
float currentSpeed = aiPath.velocity.magnitude;
|
||||
animator.SetFloat("speed", currentSpeed);
|
||||
|
||||
// Only trigger arrival logic once
|
||||
if (!hasArrived && aiPath.reachedDestination && currentSpeed < 0.01f)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(
|
||||
transform.position,
|
||||
targetPosition,
|
||||
moveSpeed * Time.deltaTime
|
||||
);
|
||||
yield return null;
|
||||
hasArrived = true;
|
||||
OnArriveAtSpot();
|
||||
aiPath.canMove = false;
|
||||
}
|
||||
transform.position = targetPosition; // Snap to exact position at the end
|
||||
moveCoroutine = null;
|
||||
// Trigger the arrival event
|
||||
OnArrivedAtSpot?.Invoke();
|
||||
}
|
||||
|
||||
private void OnArriveAtSpot()
|
||||
{
|
||||
if (animator != null)
|
||||
animator.SetTrigger("TakePhoto");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user