Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aitanar · Sep 03, 2021 at 10:41 AM · enemy ai

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. alt text

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. alt text

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.

screen-shot-2021-09-02-at-54604-pm.png (139.9 kB)
screen-shot-2021-09-02-at-50625-pm.png (109.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can someone help me make an Enemy AI script? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges