@Brian_Trotter sorry to disturb you again, but I have a bit of a funky problem. I’m trying to make a pirate system, and in my ‘BoatAttackingState.cs’, which is used to get the AI Boat to RAM the players’ boat when it gets too close, I have this function:
private void RamLastAttackerBoat(float deltaTime)
{
var targetBoat = stateMachine.GetBoatLastAttacker().GetComponentInParent<BoatStateMachine>();
if (!stateMachine.Agent.enabled) stateMachine.Agent.enabled = true; // If you turn this off, the code below won't work
if (targetBoat != null)
{
var playerOnBoat = targetBoat.GetComponentInChildren<PlayerStateMachine>();
if (playerOnBoat != null)
{
stateMachine.Agent.SetDestination(targetBoat.transform.position); // If you turn this off, the boat won't even try to reach you
Vector3 direction = stateMachine.Agent.desiredVelocity; // The direction the NavMeshAgent prefers to use to reach its goal
direction.y = 0; // Eliminate the y-axis (no flying stuff for boats, PLEASE!)
Move(direction.normalized * stateMachine.MovementSpeed, deltaTime); // If you turn this off, the boat will hug you (don't want that)
stateMachine.Agent.velocity = direction * stateMachine.MovementSpeed; // The velocity of the boat each frame
stateMachine.Agent.nextPosition = stateMachine.transform.position; // The next position for the NavMeshAgent to go towards (if it's not itself, it'll teleport. Don't want that!)
Debug.Log($"{stateMachine.gameObject.name} is moving towards {stateMachine.GetBoatLastAttacker().gameObject.name}, BoatAttackingState");
}
else
{
stateMachine.Agent.ResetPath(); // Don't try to move, because the player is not on the opponent boat (i.e: his allies are fighting you, so you should stop moving too)
Debug.Log($"{stateMachine.GetBoatLastAttacker().gameObject.name} is off the boat, resetting NavMeshAgent path, BoatAttackingState");
}
}
else
{
stateMachine.Agent.ResetPath(); // Don't try to move, because the player is not on the opponent boat (i.e: his allies are fighting you, so you should stop moving too)
Debug.Log($"TargetBoat not found. Resetting NavMeshAgent path, BoatAttackingState");
}
}
This function is supposed to get the NPC boat to RAM the players’ boat, but if the player mounts and dismounts his boat way too frequently, I am guessing the mathematics gets messy because the AI boat just either zooms its way into the players’ boat position, regardless of whether there’s colliders or not, and sometimes it literally zooms through the players’ boat to a completely random location
I also need to mention that this mostly happens when the boat gets too close, which is when it switches to the attacking state
Any idea how to fix this?
(Even the chasing state, which is the exact same formula, suffers the same problem)
Edit 1: And just disabling the NavMeshAgent does not fix the problem either…