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 Buscemi · Nov 19, 2014 at 07:41 PM · androidjavascriptai

Enemy AI; Run away from player (x and y)

I need some help with creating a script for the enemies in my game! I'm making a 2.5D game where you chase campers and try to kill them. I'm mostly done with the game, but I can't get the AI to work! I've looked around for scripts and help for a couple of days now but can't find anything that fits well with the rest of my game... Please help!

For my ground i have a flat surface rotated at 35 on the x-axis, which have worked pretty well for me so far (moving the character around and placing obstacles).

At this point I'm working on this script;

 #pragma strict
 
 //Attack button
 var attackButton : Joystick;
 
 var anim : Animator;
 var delay = 5.0;
 
 //Player can kill
 var CanKill = false;
 
 //Score
 var scoreValue : int;
 var killValue : int;
 var playerControl : PlayerControl;
 
 //AI
 var speed : int = 2;
 var Damp: float = 1.0;
 
 var isRun: boolean = false;
 var Target: Transform;
 
 var detectionRange: int = 5;
 
 private var character : CharacterController;
 
 function Start () 
 {
 
     anim = GetComponent("Animator");
     var playerControlObject : GameObject = GameObject.FindWithTag ("Player");
     
     character = GetComponent(CharacterController);
 
 
 }
 
 function WaitAndDestroy()
 {
 
     yield WaitForSeconds(delay);
         Destroy (gameObject);
         
 }
 
 function Update()
 {
 
     //Can the player kill?
     if (attackButton.tapCount == 1)
         CanKill = true;
         
     else CanKill = false; 
     
     //AI
     var FromPlayer =  Vector3(Target.position.x - transform.position.x, 0);
     
         if(FromPlayer.magnitude <= detectionRange){
             isRun = true;
         }
         
         if(FromPlayer.magnitude >= detectionRange) {
             isRun = false;
         }
         
         if(isRun) {
             RunAway();
             anim.SetBool("Walk", true);
         }
         
         else anim.SetBool("Walk", false);
         
         
 }
 
 function OnTriggerEnter (Other : Collider){
 
     if(Other.gameObject.tag == "Player" && CanKill == true) {
         playerControl.AddScore (scoreValue);
         playerControl.AddKills (killValue);
         anim.SetTrigger("Dead");
         WaitAndDestroy();
     }
     
 }
 
 function RunAway()
 {
     
     var moveDirection : Vector3 = transform.position;
     character.Move(moveDirection.normalized * speed * Time.deltaTime);
     
 }`

Which kinda works, but for some reason the character stops in the middle of the level and just runs in place... I would also like him to turn and run the other direction if I (the player) catches him and runs in front of him. (If the code is a little messy I apologize, but as I said I'm new to Javascript).

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 Jeff-Kesselman · Nov 19, 2014 at 07:44 PM

I suggest you google "Steering Behaviors".

Thats the kind of Ai you want for something like this.

For an example of steering behaviors implemented in Unity you can look at my Tag example on the page for my AI course:

http://unseenu.wikispaces.com/aiclass

The class slides contain a lecture about that code.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Ai continues to move after calling it to stop 1 Answer

Flying AI Planes 0 Answers

game won't work in build 0 Answers

Help with Enemy AI 1 Answer

AI Path and respawn help!!! 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