mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 12:57:09 +01:00
added pathfinding
This commit is contained in:
@@ -69,12 +69,13 @@ public class Person : MonoBehaviour
|
||||
movement = GetComponent<PersonMovement>();
|
||||
indicators = GetComponent<PersonIndicators>();
|
||||
|
||||
TimeManager.OnMinuteUpdate += OnMinuteUpdate;
|
||||
/* TimeManager.OnMinuteUpdate += OnMinuteUpdate;
|
||||
TimeManager.OnDayUpdate += OnDayUpdate;
|
||||
|
||||
SetBehaivorDateTimes();
|
||||
Sleep();
|
||||
Sleep();*/
|
||||
}
|
||||
/*
|
||||
void OnDayUpdate()
|
||||
{
|
||||
SetBehaivorDateTimes();
|
||||
@@ -145,5 +146,5 @@ public class Person : MonoBehaviour
|
||||
Random.Range(0, 59),
|
||||
currentDateTime.Second);
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -1,22 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class PersonMovement : MonoBehaviour
|
||||
{
|
||||
NavMeshAgent agent;
|
||||
private int currentPathIndex;
|
||||
[SerializeField] private List<Vector3> pathVectorList;
|
||||
private const float speed = 40f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
agent.updateRotation = false;
|
||||
agent.updateUpAxis = false;
|
||||
//agent.avoidancePriority = Random.Range(1, 100);
|
||||
}
|
||||
|
||||
public void SetTarget(Transform target)
|
||||
private void Update()
|
||||
{
|
||||
agent.SetDestination(target.position);
|
||||
HandleMovement();
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
SetTarget(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMovement()
|
||||
{
|
||||
if (pathVectorList != null)
|
||||
{
|
||||
Vector3 targetPosition = pathVectorList[currentPathIndex];
|
||||
if (Vector3.Distance(transform.position, targetPosition) > 1f)
|
||||
{
|
||||
Vector3 moveDir = (targetPosition - transform.position).normalized;
|
||||
|
||||
float distanceBefore = Vector3.Distance(transform.position, targetPosition);
|
||||
transform.position = transform.position + moveDir * speed * Time.deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPathIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
public Vector3 GetPosition()
|
||||
{
|
||||
return transform.position;
|
||||
}
|
||||
public void SetTarget(Vector3 targetTransform)
|
||||
{
|
||||
pathVectorList = Pathfinding.Instance.FindPath(GetPosition(), targetTransform);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user