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 Xatoku · Feb 22, 2011 at 11:02 PM · animationairunidle

Enemy AI Animation

Basically, I want my AI to play his "run" animation when running towards the player, and the "Idle" animation (or Take 001 in this case) when he isn't. Here's my script, currently, he will run up the player, and stop, but continue his run animation.

public var target : Transform; public var moveSpeed : float = 3.0; public var attackRange : float = 10.0; public var stopRange : float = 2.0; private var isRunning:boolean = true;

function Start(){ Patrol(); animation.wrapMode = WrapMode.Loop; animation["Take 001"].layer = 1; animation["run"].layer = 2; animation.Stop(); }

function Update() { if(isRunning == false){ animation.CrossFade("Take 001"); } if(isRunning == true) { animation.CrossFade("run"); } var distanceToTarget = Vector3.Distance(transform.position, target.position); if(distanceToTarget <= stopRange) { isRunning = false; animation.CrossFade("Take 001"); } }

function Patrol(){ while(true){ if( AttackRangeCheck() ){ // target object is within range if( !StopRangeCheck() ){ transform.LookAt(target); transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed); } } yield; } }

function AttackRangeCheck() : boolean{ var distanceToTarget = Vector3.Distance(transform.position, target.position); if(distanceToTarget <= attackRange) return true; else return false;
}

function StopRangeCheck() : boolean{ var distanceToTarget = Vector3.Distance(transform.position, target.position); if(distanceToTarget <= stopRange) return true; else return false;
}

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

Answer by doomprodigy · Feb 23, 2011 at 12:20 AM

Unsure on what the cause is, though make it go

function Update(){

if(isRunning == true) { animation.CrossFade("run"); }

else if(isRunning == false) {
animation.CrossFade("Take 001");
}

var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= stopRange)
{
isRunning = false;
animation.CrossFade("Take 001");
} }

That way it does either run or not.

(Unsure if it fixes your problem).

May I also add that when I script AI functions use a finte state machine. Or just use states/cases inside the same script.

Example:

enum animals { dog, cat, bird }

function Start(){
var animals : animals = animals.dog;

switch(choice) {
case animals.dog:
Debug.Log("dog");
break;
case animals.cat:
Debug.Log("cat");
break;
case animal.bird:
Debug.Log("bird");
break;
default:
Debug.Log("nothing");
break;
}

Peace, Aaron Lewis

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 Xatoku · Feb 23, 2011 at 01:06 AM 0
Share

Nope, didn't work.

avatar image Owen-Reynolds · Apr 14, 2011 at 04:31 PM 0
Share

The first insight for a "state machine" is that ins$$anonymous$$d of using isIdle, isRunning, isAttacking... you can make one variable AIstate and use 0=idle, 1=running, 2=attacking. ins$$anonymous$$d of setting run=false and attack=true, you can simply change AIstate=2. An enum is a way to "name" 0 is idle, and so on, so you can write AIstate=IDLE

avatar image
0

Answer by Owen-Reynolds · Apr 14, 2011 at 04:25 PM

Put run and Take001 on the same layer.

Each layer can play one animation, and higher layers beat lower ones. Since Run is on layer2, starting Take001 on layer 1 ALSO plays it, but the game uses only Run. If you leave them on the same layer, Take001 will automatically cancel (and fade from) Run. You could stop run before starting Take001, but that's looks worse and is more work.

Higher layers are good for a non-looping animations, like shoot. When it stops, the lower level one, which was always secretly playing, "comes back."

You can skip setting layers for Run/Take001, since the starting layer is 0. I also prefer to set Loop individually for each animation clip, in the Inspector for the model. That way you can set shoot or die to non-looping.

See http://unity3d.com/support/documentation/Manual/Animations.html for a better explanation.

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

No one has followed this question yet.

Related Questions

Enemy Animation Freezing in first frame 2 Answers

where do i call the Run and Idle animation in this script 2 Answers

play animation when moving,and idle animation when stand still 1 Answer

Idle Animations, Player Walking 0 Answers

Player plays walking animation if I press a and d at the same time while standing still 2 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