- Home /
Help with Enemy Ai not going back to patroling after attack ,help with enemy ai not going back to regular movement after attack
So im creating a board game type game in unity3d. i have an animated crocodile swimming in a lake back and forth during the whole game. when a player lands on a specific node the node calls the attack() function from the crocSwim script that the crocodile is attached to and the crocodile stops swimming between the two waypoints i have in the water for him and moves to where that node is to attack the player. right now i have him swimming back and forth between the waypoints fine and i have an empty game object called attack which is placed where that node is that the player is standing on top of that i want him to go to and attack. when the player lands on that point he does successfully go to that point but i cant seem to get him to then go back after to his default swimming waypoints. he just keeps weirdly going around the attack point. i also wanted to make it so he goes to that attack point, does his attack animation when he is there and then once he plays that attack animation once, he turns and goes back to the water and continues swimming like he did in the beginning. Im new to coding and have been stumped on this forever and would greatly appreciate any help! Thank you so so much. Here are my scripts. I watched a lot of tutorials and tried to piece the parts i wanted together so I apologize if im doing it very weirdly wrong. Thats why im here.
my crocodile is a child of my Enemy game object. the crocodile itself only has the animator controller attached to it.
Everything else is attached to my Enemy gameobject which is just an empty parent game object. attached to the Enemy game object is a capsule collider, a navmesh agent, a rigidbody, the CrocSwim script, the Croc script, and the Crocodile_Animation script. in the inspector for the crocSwim script I have the speed set to 0.2 and i have the 2 Waypoints in the water attached for when he is swimming. Then in the inspector for the Croc script i have the radius set to 0.1 and in the Interaction Transform slot i dragged its own Transform into it.
CROCSWIM SCRIPT using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class crocSwim : MonoBehaviour {
NavMeshAgent agent;
private int waypointIndex;
public float lookRadius = 0.7f;
private float dist;
public float speed;
public bool playerInTerritory = false;
Transform target;
public Transform[] waypoints;
void Start()
{
waypointIndex = 0;
transform.LookAt(waypoints[waypointIndex].position);
target = AttackPoint.instance.attack.transform;
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (playerInTerritory == false)
{
dist = Vector3.Distance(transform.position, waypoints[waypointIndex].position);
if(dist < 0.2f)
{
IncreaseIndex();
}
Patrol();
}
if (playerInTerritory == true)
{
Attack();
}
}
private void Patrol()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
private void IncreaseIndex()
{
waypointIndex++;
if(waypointIndex >= waypoints.Length)
{
waypointIndex = 0;
}
transform.LookAt(waypoints[waypointIndex].position);
}
public void Attack()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
//attack the target
FaceTarget();
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
CROC SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Croc : Interactable
{
public override void Interact()
{
base.Interact();
// Attack
}
CROCODILE_ANIMATION SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Crocodile_Animation : MonoBehaviour
{
const float locomotionAnimationSmoothTime = .1f;
NavMeshAgent agent;
Animator animator;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
float speedPercent = agent.velocity.magnitude / agent.speed;
animator.SetFloat("speedPercent", speedPercent, locomotionAnimationSmoothTime, Time.deltaTime);
}
}
Then the empty game object attack only has a sphere collider attached to it. Then I have another empty game object called attackPointManager at (0,0,0) with this AttackPoint script attached to it and in the inspector the attack game object dragged into the gameobject slot.
ATTACK POINT SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackPoint : MonoBehaviour
{
#region Singleton
public static AttackPoint instance;
void Awake()
{
instance = this;
}
#endregion
public GameObject attack;
}
Sorry this is so long just wanted to be as thorough as possible.,So im creating a board game type game in unity3d. i have an animated crocodile swimming in a lake back and forth during the whole game. when a player lands on a specific node the node calls the attack() function from the crocSwim script that the crocodile is attached to and the crocodile stops swimming between the two waypoints i have in the water for him and moves to where that node is to attack the player. right now i have him swimming back and forth between the waypoints fine and i have an empty game object called attack which is placed where that node is that the player is standing on top of that i want him to go to and attack. when the player lands on that point he does successfully go to that point but i cant seem to get him to then go back after to his default swimming waypoints. he just keeps weirdly going around the attack point. i also wanted to make it so he goes to that attack point, does his attack animation when he is there and then once he plays that attack animation once, he turns and goes back to the water and continues swimming like he did in the beginning. Im new to coding and have been stumped on this forever and would greatly appreciate any help! Thank you so so much. Here are my scripts. I watched a lot of tutorials and tried to piece the parts i wanted together so I apologize if im doing it very weirdly wrong. Thats why im here.
my crocodile is a child of my Enemy game object. the crocodile itself only has the animator controller attached to it.
Everything else is attached to my Enemy gameobject which is just an empty parent game object. attached to the Enemy game object is a capsule collider, a navmesh agent, a rigidbody, the CrocSwim script, the Croc script, and the Crocodile_Animation script. in the inspector for the crocSwim script I have the speed set to 0.2 and i have the 2 Waypoints in the water attached for when he is swimming. Then in the inspector for the Croc script i have the radius set to 0.1 and in the Interaction Transform slot i dragged its own Transform into it.
CROCSWIM SCRIPT using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class crocSwim : MonoBehaviour {
NavMeshAgent agent;
private int waypointIndex;
public float lookRadius = 0.7f;
private float dist;
public float speed;
public bool playerInTerritory = false;
Transform target;
public Transform[] waypoints;
void Start()
{
waypointIndex = 0;
transform.LookAt(waypoints[waypointIndex].position);
target = AttackPoint.instance.attack.transform;
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (playerInTerritory == false)
{
dist = Vector3.Distance(transform.position, waypoints[waypointIndex].position);
if(dist < 0.2f)
{
IncreaseIndex();
}
Patrol();
}
if (playerInTerritory == true)
{
Attack();
}
}
private void Patrol()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
private void IncreaseIndex()
{
waypointIndex++;
if(waypointIndex >= waypoints.Length)
{
waypointIndex = 0;
}
transform.LookAt(waypoints[waypointIndex].position);
}
public void Attack()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
//attack the target
FaceTarget();
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
CROC SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Croc : Interactable
{
public override void Interact()
{
base.Interact();
// Attack
}
CROCODILE_ANIMATION SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Crocodile_Animation : MonoBehaviour
{
const float locomotionAnimationSmoothTime = .1f;
NavMeshAgent agent;
Animator animator;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
float speedPercent = agent.velocity.magnitude / agent.speed;
animator.SetFloat("speedPercent", speedPercent, locomotionAnimationSmoothTime, Time.deltaTime);
}
}
Then the empty game object attack only has a sphere collider attached to it. Then I have another empty game object called attackPointManager at (0,0,0) with this AttackPoint script attached to it and in the inspector the attack game object dragged into the gameobject slot.
ATTACK POINT SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackPoint : MonoBehaviour
{
#region Singleton
public static AttackPoint instance;
void Awake()
{
instance = this;
}
#endregion
public GameObject attack;
}
Sorry this is so long just wanted to be as thorough as possible.
Your answer
Follow this Question
Related Questions
Enemy Combat AI 1 Answer
Kill character on impact 1 Answer
Enemy Jump AI Problem 0 Answers
Add navmesh to enemy 0 Answers