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 Tracey P · Oct 23, 2011 at 10:58 PM · aienemyenemy aiattackattacking

Enemy AI problems

Well I got my slime enemy moving toward the character but the problem is that he keeps moving. I want to make it so that when he enters a certain distance from the character he will stop moving and start attacking the character. script is:

 var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;
private var walkspeed: float = -3.0;
private var stop: float = 0;
var range : float = .5;
var character : CharacterController; 
var target : CharacterController;
var direction : float = -1;
function Update () {
    distance = Vector3.Distance(transform.position, target.transform.position); 
if(distance >= 1)    
{   
    character.Move(Vector3.forward * walkspeed * direction * Time.deltaTime);
    animation.Play(walkanimation.name);
}
if(distance <= 1){
    animation.Play(attackanimation.name);
    character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
}
}

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 Tracey P · Oct 23, 2011 at 11:02 PM 0
Share

Oh and thanks for any help

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by timsk · Oct 24, 2011 at 10:46 AM

You nearly have it.

The main problem i can see is in this bit:

 if(distance <= 1){
     animation.Play(attackanimation.name);
     character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
 }

Just for starters, a distance of 1 is tiny, i'm not sure of the scale your using in your game but you might want something slightly bigger.

Now, we know we do not want to move the enemy if its in range, so lets delete the character.Move bit and just play the animation, then run the attack function:

 var attackanimation: AnimationClip;
 var walkanimation: AnimationClip;
 var dieanimation: AnimationClip;
 
 private var walkspeed: float = 3.0; //Don't understand why this was negative.
 
 var range : float = .5;
 var character : CharacterController; 
 var target : Transform; // lets cache the transform, rather than use a lookup.
 var direction : float = -1; //why is this here?

 function Awake()
 {
 target = GameObject.Find("your target").GetComponent(Transform);
 }
 
 function Update () {
 distance = Vector3.Distance(transform.position, target); //no more lookup for target. 
 var forward : Vector3 = transform.TransformDirection(Vector3.forward);
 
     if(distance >= range)//you have a range variable, lets use it!    
     {   
         character.SimpleMove(forward * speed); //SimpleMove is simpler :).
         animation.CrossFade(walkanimation.name); //let's make the anims look better.
     }
 
     if(distance <= range)//you have a range variable, lets use it!
     {
         animation.CrossFade(attackanimation.name); //crossfading again.
         DamageorAttackFunction();
     }

I hope the comments in the code help you to understand it. Might be 1 or 2 errors as i haven't tested it but nothing you shouldn't be able to solve.

Let me know how it goes!

Comment
Add comment · Show 2 · 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 Tracey P · Nov 14, 2011 at 01:20 AM 0
Share

distance = Vector3.Distance(transform.position, target); this dosnt work

avatar image Tracey P · Nov 14, 2011 at 01:35 AM 0
Share

Still not working

avatar image
0

Answer by Tracey P · Nov 14, 2011 at 04:02 AM

Ok hey guys I got him working with the help of Timsk, but now Im trying to figure out how to make him rotate around and chase me after I dodge him. I cant seem to figure it out. Im looking through the scripting guide to find it, but I could use some help. Thanks for the Help. Heres the script

 var attackanimation: AnimationClip;
 
 var walkanimation: AnimationClip;
 
 var dieanimation: AnimationClip;
 
 var distance;
 
 
 
 
 
 private var walkspeed: float = -3.0; //Don't understand why this was negative.
 
 
 
 var range : float = .5;
 
 var character : CharacterController; 
 
 var target : Transform; // lets cache the transform, rather than use a lookup.
 
 var direction : float = -1; //why is this here?
 
 function Awake()
 
 {
 
 target = GameObject.Find("your target").GetComponent(Transform);
 
 }
 
 
 
 function Update () {
 
     distance = Vector3.Distance(transform.position, target.transform.position); 
 
     }
 
 
 
 function LateUpdate () {
 
 var forward : Vector3 = transform.TransformDirection(Vector3.forward);
 
     if(distance >= range)//you have a range variable, lets use it!    
 
     {   
 
         character.SimpleMove(forward * walkspeed); //SimpleMove is simpler :).
 
         animation.CrossFade(walkanimation.name); //let's make the anims look better.
 
     }
 
 
 
     if(distance <= range)//you have a range variable, lets use it!
 
     {
 
         animation.CrossFade(attackanimation.name);
 
     }
 
 }
Comment
Add comment · Show 2 · 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 timsk · Nov 14, 2011 at 08:56 AM 0
Share

target.transform.position is redundant, target is a transform, so just use target.position

you have posted this as an answer to your own question, however, it is in fact a new question and should have been posted as such.

From my understanding you want the character to look at the player. For this you might want to look into Transform.LookAt

avatar image Tracey P · Nov 14, 2011 at 11:10 PM 0
Share

Hey thanks and I know I just wanted to make it as easy as possible for those who wish to view this after it has worked for me

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Different enemy types script design 1 Answer

Problem with enemy AI 1 Answer

How do I make my AI attack in the FPS tutorial? 0 Answers

Melee range AI 1 Answer

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