Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SuperjSav · Dec 19, 2016 at 06:49 AM · scripting problemscripting beginner

Making enemy AI waypoints HELP!

Alright so i am making a little horror game and i want the enemy to patrol the area, around objects and up and down hills and stuff (my terrain isnt just flat). And when i go in the enemies line of sight or i get to close it comes after me and when i get to far it continues patrolling. I am using this code using UnityEngine; using System.Collections;

public class Chase : MonoBehaviour {

 public Transform player;
 public Transform head;
 static Animator anim;

 string state = "patrol";
 public GameObject[] waypoints;
 int currentWP = 0;
 float rotSpeed = 0.2f;
 float speed = 0.02f;
 float accuracyWP = 5.0f;

 // Use this for initialization
 void Start ()
 {
     anim = GetComponent<Animator>();
 }

 // Update is called once per frame
 void Update()
 {
     Vector3 direction = player.position - this.transform.position;
     direction.y = 0;
     float angle = Vector3.Angle(direction, head.up);

     if(state == "patrol" && waypoints.Length > 0)
     {
         anim.SetBool("isIdle", false);
         anim.SetBool("isWalking", true);
         if(Vector3.Distance(waypoints[currentWP].transform.position, transform.position) < accuracyWP)
         {
             currentWP++;
             if(currentWP >= waypoints.Length)
             {
                 currentWP = 0;
             }
         }

         //rotate towards waypoint
         direction = waypoints[currentWP].transform.position - transform.position;
         this.transform.rotation = Quaternion.Slerp(transform.rotation,
                              Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
         this.transform.Translate(0, 0, Time.deltaTime * speed);
     }


     if (Vector3.Distance(player.position, this.transform.position) < 20 && (angle < 30 || state == "pursuing"))
     {


         state = "pursuing";
         this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                     Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);

         
         if (direction.magnitude > 5)
         {
             this.transform.Translate(0,0,Time.deltaTime * speed);
             anim.SetBool("isWalking", true);
         }
         else
         {
             anim.SetBool("isWalking", false);
         }

     }
     else
     {
         anim.SetBool("isIdle", true);
         anim.SetBool("isWalking", false);
         state = "patrol";
     }

 }

}

I got it off this video right hear. https://www.youtube.com/watch?v=OmoKw1ikAi8&t=777s The problem is that since my terrain isnt flat the enemy just floats a little bit above the ground and also i have 19 waypoints that zig zag in and out around the "town". So since the ground isnt flat and theirs obstacles the enemy either doesnt move or he floats around. I have tried adding a rigid body and checking gravity but all that does is makes the enemy fall over. So what i need is for someone to maybe edit this script for me so that the enemy ai will be able to go to all the waypoints, not float and stay on the ground, and when i get into the line of site he will stop patrolling and chase me and when i get to far he will stop and continue patrolling. I am not good at scripting so if you just type what i need without adding it to the whole script i wont know what to do Thanks!!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ericbegue · Dec 19, 2016 at 12:35 PM

You should not move your enemy by directly translating its position. Unity has a built-in navigation system, which essentially makes your AI aware of the environment and decide which path to take to move from point A to point B by taking into account possible ways and obstacles. It will simplifies your problem a lot if you make use of NavMesh and NavMeshAgent.

Your AI is based on a Finite State Machine (FSM). FSM is simple to understand and works great for simple AI. But it becomes unmanageable for more complex AIs. Behaviour Tree (BT) is a good tool for handling more complex AIs. Have a look at Panda BT (www.pandabehaviour.com), it's a scripting framework based on Behaviour Tree. The package contains several examples, include patrol-chase-attack behaviours. I'm the author. I'd be glad to help if you have any question about using this package or about Behaviour Tree in general. You're welcome on this forum thread.

Comment
Add comment · Show 6 · Share
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
avatar image SuperjSav · Dec 19, 2016 at 04:54 PM 0
Share

Thanks so much i may need help with the BT but i will see what i can do with it first also what should i have in the script then or should i just delete that script and use th BT.

avatar image ericbegue SuperjSav · Dec 19, 2016 at 05:51 PM 0
Share

Defining a Behaviour Tree with Panda BT consists of 2 steps:

  1. Define 'tasks' in a $$anonymous$$onoBehaviour.

  2. Use these 'tasks' as building blocks to write a BT script.

Applying this to your problem, you would need first to define a task that makes the Nav$$anonymous$$esh agent move to some given destinations.

 using UnityEngine;
 using Panda; // We are using Panda BT
 
 public class Nav$$anonymous$$eshBT : $$anonymous$$onoBehaviour
 {
     public Transform[] locations; // Some locations to move to
 
     Nav$$anonymous$$eshAgent nav$$anonymous$$eshAgent;
     void Start()
     {
         nav$$anonymous$$eshAgent = GetComponent<Nav$$anonymous$$eshAgent>();
     }
 
     [Task] // A task to move the Nav$$anonymous$$eshAgent toward a destination.
     void $$anonymous$$oveTo(int locationIndex)
     {
         if (Task.current.isStarting)
         {// When the task is starting, we set the destinaiton.
             nav$$anonymous$$eshAgent.destination = locations[locationIndex].position;
         }
         else
         if (nav$$anonymous$$eshAgent.remainingDistance <= nav$$anonymous$$eshAgent.stoppingDistance)
         {// The task is done when we arrived at destination.
             Task.current.Succeed();
         }
     }
 }

 

Then you can use this task from a BT script to define how your AI behaves. For instance we want the AI to move back and forth between the first and the second location. Then, the BT script is:

 tree("Root")
     sequence
         $$anonymous$$oveTo(0)
         $$anonymous$$oveTo(1)
 

Don't hesitate to post on this forum thread if you have any question.

avatar image SuperjSav ericbegue · Dec 20, 2016 at 03:23 AM 0
Share

Should i delete the script that i have then and just use BT?

Show more comments
avatar image ericbegue SuperjSav · Dec 20, 2016 at 11:29 AM 0
Share

So, basically, you are asking me to write this script for you in exchange of credits on your youtube channel?...

What is your youtube channel?

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

104 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

Related Questions

NavMesh of an automatically generated maze 0 Answers

Displaying Text on Touch Event 0 Answers

Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers

unity visual studios not working properly 0 Answers

Finishing up my 2D quiz game [Last Problem] 0 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