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 bobgrants · Jul 24, 2013 at 03:51 PM · aiattack

How to improve my enemy behavior ?

Hello guys,

I'm working on a space shooter, top view, in which you control your character in an open world (or at least big maps).

There is enemy trying to take down the player and to make them chase the player I use this script :

 #pragma strict
 
 var target : Transform; //the enemy's target
 var moveSpeed = 500; //move speed
 var rotationSpeed = 0.5; //speed of turning
 var enemyTransform : Transform; //current transform data of this enemy
 private var timer = 0.0;
 
 var enemyForward : Vector3;
 enemyForward = target.transform.position;
 enemyForward.x += 3000;
 
 
  
 function Awake()
 {
     enemyTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
  
  
 }
  
 function Update () {
     //rotate to look at the player
     enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
  
     //move towards the player
     enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
 
 if(enemyShooter = true)
 {
 
 enemyTransform.Translate(target.position + 3000, 0, target.position + 3000);
 } 
     
 }
 
 // Reset all forces after a collision with player or other gameObject with collider
 function FixedUpdate () {
 
         
 timer += Time.deltaTime;
         
         
 if(timer >= 15.0)
 {
 rigidbody.velocity = Vector3(0,0,0);
 rigidbody.angularVelocity = Vector3(0,0,0);
 timer = 0.0;
 }
 }


My problem is, if the player stop moving, the enemy catch up, and then just turn around the player. I think it is behaving like that as it can't reduce its speed, and is using the maximum rotation speed allowed.

More than a solution I'm looking for advice.

Do you think it is best to let the enemy regulate its moveSpeed and rotationSpeed by itself, or is it better to look for an AI script telling the enemy to come close to the player, slow down, shoot, go away, and then start attack routine again?

Any ideas and advice more than welcome.

Thank you

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

2 Replies

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

Answer by bobgrants · Jul 27, 2013 at 01:14 PM

Hey !

Sorry for the late answer.

Here is the code I ended up with :

 function Awake()
 {
     enemyTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
   //target = GameObject.FindWithTag("Player").transform; //target the player
 
 
 }
 
 
  
 function chasePlayer() {
    
 var moveAway = Vector3(Random.Range(3000, 6000), 0, Random.Range(3000, 6000)); 
     
       
   //rotate to look at fly away point
     enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(moveAway - enemyTransform.position), rotationSpeed*Time.deltaTime);
  
     //move towards fly away point
     enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
     
 }
 
 
 
 
 
 function flyAway() {  
 
   
      //rotate to look at the player
     enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.position - enemyTransform.position), rotationSpeed * Time.deltaTime);
  
     //move towards the player
     enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
 }
 
 
 
 
 function enemyBehavior() {
 
 var distance = Vector3.Distance(target.position, enemyTransform.position);
 
 if(distance >= 2000){   //Value is fire range
 
 flyAway();
 
 }
 
 else {
 
 chasePlayer();
 
 }
 }
 
 
 
 function FixedUpdate() {
 
         
 timer += Time.deltaTime;
         
         
 if(timer >= 10.0)
 {
 rigidbody.velocity = Vector3(0,0,0);
 rigidbody.angularVelocity = Vector3(0,0,0);
 timer = 0.0;
 }
 
 enemyBehavior();
 
 }


It is working, I have to tune up the " if(distance >= 2000){ //Value is fire range " bit.

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
avatar image
0

Answer by Mikael-Gyth · Jul 24, 2013 at 04:17 PM

I'd say both. You would want to seperate the goal from the execution. Having one AI that tell your enemy what to do, and then let the Enemy figure out how to do that itself. That way you can have conditions and actions in your AI (if this, do that, etc.) And you can have commands like MoveTo, KeepDistance, Shoot, Etc. on the enemy.

So your code could be like this:

 if(distance < firingDistance){

 KeepDistance();
 Fire();
 FlyAway();
 else if(!flyingAway)
 catchUp();


This could ofcourse be implemented as a BehaviorTree for even more advanced behavior.

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

16 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

Related Questions

Zombie attack script help 1 Answer

Ai Zombie Melee Attack script. 5 Answers

Enemy behavior is not working after the enemy has spawned. 1 Answer

I'm still having Trouble with this AI script 2 Answers

Creating basic enemy animation and attacking 4 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