Files
real-shooter/Assets/Scripts/Character/MovementController.cs
Andrey Gumirov aae98595d3 Small fizes
2022-04-19 19:24:53 +07:00

48 lines
1.4 KiB
C#

using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class MovementController : MonoBehaviour
{
public NavPoint currentPosition { get; set; }
private Dictionary<int, NavPoint> navPoints = new Dictionary<int, NavPoint>();
[SerializeField] private NavMeshAgent navMeshAgent;
private void Start()
{
navMeshAgent.speed = SettingsReader.Instance.GetSettings.movementSpeed;
foreach (var np in MapManager.navPoints) {
navPoints[np.PointId] = np;
}
}
public void MoveToPointById(int id)
{
if (!navPoints.ContainsKey(id))
{
Debug.LogWarning("PIDOR");
return;
}
goToNextNavPoint(navPoints[id]);
}
public void MoveToRandomPoint()
{
// Debug.Log(MapManager.navPoints == null);
goToNextNavPoint(MapManager.navPoints[Random.Range(0, MapManager.navPoints.Count)]);
}
public List<NavPoint> getPointsCandidate()
{
return MapManager.navPoints
.Where(point => (currentPosition.position - point.position).magnitude < SettingsReader.Instance.GetSettings.movementDistance)
.ToList();
}
public void goToNextNavPoint(NavPoint destination) =>
navMeshAgent.SetDestination(destination.position);
}