TowerDefenseGame/Assets/Plugins/NavMeshPlus/NavMeshComponents/Scripts/RotateAgentInstantly.cs
Nico 0ef60e5828 Add third party plugin NavMeshPlus
Add A* pathing to enemies
Add choice between havving Goblers chase player as priority or go to crystal
2025-07-09 19:14:28 -07:00

40 lines
1.4 KiB
C#

using UnityEngine;
using UnityEngine.AI;
//***********************************************************************************
// Contributed by author @Lazy_Sloth from unity forum (https://forum.unity.com/)
//***********************************************************************************
namespace NavMeshPlus.Extensions
{
public class RotateAgentInstantly: IAgentOverride
{
public RotateAgentInstantly(NavMeshAgent agent, AgentOverride2d owner)
{
this.agent = agent;
this.owner = owner;
}
private NavMeshAgent agent;
private AgentOverride2d owner;
private Vector3 nextWaypoint;
public void UpdateAgent()
{
if (agent.hasPath && agent.path.corners.Length > 1)
{
if (nextWaypoint != agent.path.corners[1])
{
RotateToPoint(agent.path.corners[1], agent.transform);
nextWaypoint = agent.path.corners[1];
}
}
}
private static void RotateToPoint(Vector3 targetPoint, Transform transform)
{
Vector3 targetVector = targetPoint - transform.position;
float angleDifference = Vector2.SignedAngle(transform.up, targetVector);
transform.rotation = Quaternion.Euler(0, 0, transform.localEulerAngles.z + angleDifference);
}
}
}