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 GEWLAR · Sep 26, 2013 at 09:09 AM · c#airandomenemy

Make AI walk around randomly until Player is Seen. (C#)

I have managed to make my AI follow me around as soon as he sees me but he doesn't stop when I get out of sight. I also want the enemy (AI) walk around randomly before he sees me and start walking around randomly again when I get out of Sight. Everything is in a maze so the enemy needs to know where the walls are so he doesn't walk trough them.

At the moment I'm using the behavior tree system of RAIN indie to let the AI follow the player.

Here's what I've got so far:

 <behaviortree name="Enemy" precondition="" repeatuntil="" debug="False"><sequencer name="root" precondition="" repeatuntil="" debug="True"><detect name="detect 241" precondition="" repeatuntil="" debug="True" sensor="sight" aspect="target" variable="playerpos" /><move name="move 2403" precondition="" repeatuntil="" debug="True" movetarget="playerpos" movespeed="-1" looktarget="playerpos" lookspeed="-1" animation="" animationlayer="0" animationwrapmode="default" animationbasespeed="2" /></sequencer><variable name="CanSee" initialvalue="0" /></behaviortree>

I would also apreciate if anyone could tell me how to do this without using an asset and just a script. Thanks for your Help.

I'm trying to do this now with a script. It's like that:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     
     private GameObject Player;
     private bool CanSee = false;
     
     void update()
     {    if(CanSee=true)
         {
             follow();
         }
         else{
             random();
         }
     }
     
     private void follow()
         {    //lets the enemy follow the player
             RAIN.Path.MoveLookTarget.SaveLastPosition(Player);
 
         }
     
     private void random()
     {    //lets the enemy move randomly
         
     }
 }

Of course it knows now when it can see the player. Now I'm trying to let it walk to the player.

EDIT: I've found now a class called RAIN.Motion.Instantaneous.SB_Wander it should let an object wander around. But I don't know how to use this in a script. Can someone help me?

Comment
Add comment · Show 1
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 vexe · Oct 07, 2013 at 08:59 AM 0
Share

Well, since not all of us use RAIN, we can't give you RAIN-specific help. But everything should be in the documentation, read it well. If nothing's there, maybe you should even send a msg to the author. Lurk around the script it self, try to figure out how it works. I also think you should ask in a separate question, it would be better.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Sep 26, 2013 at 09:46 AM

I don't know about RAIN. But for randomly walking around, you could just create a couple of empty gameObjects pre-set their positions in your scene and maybe let your enemy store them in an array or list at his disposal and choose randomly between them (depends on what 'random' means to you)

 nextNode = nodes[Random.Range(0, nodes.Length)];

  • Careful with Random.Range though, in the int version the beginning limit is inclusive and the end is exclusive. See.

  • Of course, selecting a node randomly and heading to it in a trivial way isn't very sufficient, you gotta have some functions/methods for him to help him find his path, what if for example two nodes weren't exposed? (walls between them or something)... going into other deep waters there.

About stopping when he sees you, I don't know how you set him up to detect you, but it should be done via a distance variable that you set, and a field of view angle. So if you're in that distance && inside that fov he sees you, otherwise not and go back to his 'idle' state. Btw you should have states for your enemy stored as enums - Attacking, idle, Spectating, etc.

Couple of helpful links:

  • Here's a very nice article about field of view and dot product.

  • Here's a very nice tutorial on AI and enemy states.

Comment
Add comment · Show 9 · 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 GEWLAR · Sep 26, 2013 at 09:53 AM 0
Share

First thank you. The problem is when I use gameObjects our mazes are often realy big so it needs a lot of objects. But if there's no other option we have to do it that way. I'm trying it out with the distance variable.

avatar image vexe · Sep 26, 2013 at 09:56 AM 0
Share

If you have a maze, then going randomly between nodes -in a basic fashion- isn't going to work. What kind of maze do you have? You might be looking for a maze-solving algorithm in that case.

avatar image GEWLAR · Sep 26, 2013 at 09:58 AM 0
Share

The player has to get out of the maze befor an enemy hits him. The enemys just have to follow the player and move randomly.

avatar image vexe · Sep 26, 2013 at 10:11 AM 0
Share

Please give us more info. What code is there currently for the enemy AI? - how is he spotting you? - you said he doesn't stop when he sees you, did you write anything for him to stop?

avatar image GEWLAR · Sep 26, 2013 at 10:57 AM 0
Share

No i don't have any code to let him stop. As i said at the moment I'm using the behavior tree system of RAIN indie to let him detect the player. So he knows when he can see the player. There's a sensor which marks the player as targeted and then let the enemy follow the target. Now the enemy doesn't need to stop walking he just has to get in the state random walk.

Show more comments
avatar image
0

Answer by SAE_Superman · Oct 07, 2013 at 06:29 PM

Collision detection is your friend. Simply add a trigger collision volume to your characters. This way you can detect when characters enter, stay in or exit that "field of view". Inside the OnEnter, turn on the follow behavior and inside the OnExit, turn on the wander behavior.

Being a maze, you might be able to "see through walls", but you can fix this by having a unique ID for each "corrider" or "hallway" and do a simple ID check to see if who you are seeing is in the same hallway. There are other ways (better but also more complex) to fix this issue too though.

Comment
Add comment · 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

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

17 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

Related Questions

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers

How to make basic AI in a 2d game? 4 Answers

Unity 3D Audio Clip Disabled 1 Answer


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