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 /
This question was closed Sep 27, 2015 at 01:13 PM by RedDevil for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RedDevil · Nov 19, 2013 at 11:40 AM · animationai

Unity3D AI using animations

Hi!I have been learning Unity3D for the past weeks and i stumbled upon a problem. I dont know how to properly make the enemy play the animations. Here is the code i have up to now:

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var attackRange = 2.0;
 var MoveSpeed = 5.0;
 var Damping = 6.0;
 var attackRepeatTime = 2.0;
 private var attackTime : float;
 var chaseRange = 15.0;
 var controller : CharacterController;
 var gravity : float = 20.0;
 var TheDamage = 40.0;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 //anim vars
 var AttackAnim : Transform;
 var WalkAnim : Transform;
 var RunAnim : Transform;
 
 
 function Start()
 {
  attackTime = Time.time;
 }
 
 function Update()
 {
   if(RespawnMenuV2.playerIsDead == false)
   {
       Distance = Vector3.Distance(Target.position,transform.position);
       
       if(Distance < lookAtDistance)
       {
         lookAt();
       }
       if(Distance < attackRange)
       {
        attack();
       }
       
       else if(Distance < chaseRange)
       {
         chase(); 
         WalkAnim.animation.CrossFade("Walk");
       }
   }
 }
 
 function lookAt()
 {
   
   var rotation = Quaternion.LookRotation(Target.position - transform.position);
   transform.rotation = Quaternion.Slerp(transform.rotation , rotation, Time.deltaTime * Damping);
 
 }
 
 function chase()
 {
  WalkAnim.animation.Play("Walk");
  MoveDirection = transform.forward;
  MoveDirection *= MoveSpeed;
  
  MoveDirection.y-=gravity * Time.deltaTime;
  controller.Move(MoveDirection * Time.deltaTime);
 }
 
 function attack()
 {
  if(Time.time > attackTime)
  {
   AttackAnim.animation.Play("Attack");
   Target.SendMessage("ApplyDamage",TheDamage,SendMessageOptions.DontRequireReceiver);
   Debug.Log("TheEnemyHasAttacked!");
   attackTime = Time.time + attackRepeatTime;
   AttackAnim.animation.CrossFade("Attack");
  }
 }
 
 function ApplyDamage()
 {
  RunAnim.animation.Play("Run");
  chaseRange += 30;
  MoveSpeed += 2;
  lookAtDistance += 40;
  RunAnim.animation.CrossFade("Run");
 }

So I have 3 animations that i want to attach to the enemy AI:Walk,Run,Attack. I want that when I get close to the enemy he walks up to me and starts atacking.If i hit the enemy( ApplyDamage()) then i want him to use the run animation to chase me. My Unity3D knowledge is not very large so if you guys chould help me I would really apreciate it.I tryed using var AttackAnim : AnimationClip but i never worked with those so I dont know how it would be done.Thank you

Comment
Add comment · Show 2
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 RedDevil · Nov 19, 2013 at 12:41 PM 0
Share
 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var attackRange = 2.0;
 var $$anonymous$$oveSpeed = 3.0;
 var Damping = 6.0;
 var attackRepeatTime = 2.0;
 private var attackTime : float;
 var chaseRange = 15.0;
 var controller : CharacterController;
 var gravity : float = 20.0;
 var TheDamage = 40.0;
 private var $$anonymous$$oveDirection : Vector3 = Vector3.zero;
 
 //anim vars
 var AttackAnim : AnimationClip;
 var WalkAnim : AnimationClip;
 var RunAnim : AnimationClip;
 
 
 function Start()
 {
  attackTime = Time.time;
 }
 
 function Update()
 {
   if(Respawn$$anonymous$$enuV2.playerIsDead == false)
   {
       Distance = Vector3.Distance(Target.position,transform.position);
       
       if(Distance < lookAtDistance)
       {
         lookAt();
       }
       if(Distance < attackRange)
       {
        attack();
       }
       
       else if(Distance < chaseRange)
       {
         chase(); 
         animation.CrossFade(WalkAnim.Walk);
         
       }
   }
 }
 
 function lookAt()
 {
   
   var rotation = Quaternion.LookRotation(Target.position - transform.position);
   transform.rotation = Quaternion.Slerp(transform.rotation , rotation, Time.deltaTime * Damping);
 
 }
 
 function chase()
 {
  animation.Play(WalkAnim.Walk);
  $$anonymous$$oveDirection = transform.forward;
  $$anonymous$$oveDirection *= $$anonymous$$oveSpeed;
  
  $$anonymous$$oveDirection.y-=gravity * Time.deltaTime;
  controller.$$anonymous$$ove($$anonymous$$oveDirection * Time.deltaTime);
 }
 
 function attack()
 {
  if(Time.time > attackTime)
  {
   animation.Play(AttackAnim.Attack);
   Target.Send$$anonymous$$essage("ApplyDamage",TheDamage,Send$$anonymous$$essageOptions.DontRequireReceiver);
   Debug.Log("TheEnemyHasAttacked!");
   attackTime = Time.time + attackRepeatTime;
   animation.CrossFade(AttackAnim.name);
  }
 }
 
 function ApplyDamage()
 {
  animation.Play(RunAnim.Run);
  chaseRange += 30;
  $$anonymous$$oveSpeed += 2;
  lookAtDistance += 40;
  animation.CrossFade(RunAnim.Run);
  Debug.Log("Applyed D$$anonymous$$G is ON!");
 }

These are the modifications to the code after your first comment.

avatar image RedDevil · Nov 19, 2013 at 02:25 PM 0
Share

forgot to say but the issue with the weapon switching and animation not playing! when im starting the game i get imediately :Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) WeaponSwitching:SelectWeapon(Int32) (at Assets/Scripts/WeaponSwitching.js:80) WeaponSwitching:Awake() (at Assets/Scripts/WeaponSwitching.js:9)

I solved this problem by enabling the "standard weapon".It was left to disabled

1 Reply

  • Sort: 
avatar image
0

Answer by tanoshimi · Nov 19, 2013 at 11:43 AM

 var AttackAnim : Transform;
 var WalkAnim : Transform;
 var RunAnim : Transform;

need to be:

 var AttackAnim : AnimationClip;
 var WalkAnim : AnimationClip;
 var RunAnim : AnimationClip;

Then drag your three animation clips onto those three slots in the inspector and give it another go. There may well be other issues, but fix that first and let us know how you get on.

Comment
Add comment · Show 7 · 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 tanoshimi · Nov 19, 2013 at 11:48 AM 0
Share

Also, try replacing all references like this:

 RunAnim.animation.Play("Run");
 AttackAnim.animation.CrossFade("Attack");

with references like this:

 animation.Play(RunAnim.name);
 animation.CrossFade(AttackAnim.name);
avatar image RedDevil · Nov 19, 2013 at 12:38 PM 0
Share

Thank you for the prompt response. I am having the following isues: First the enemy comes to me like sliding and is not really playing the walk animation! 2.He does keep playing the atack animation like in a loop when he is close to me.I only want this to happend every time he atacks.And that is set to 2 seconds.So I want him to atack then stop the animation and 2 seconds later do it again. 3.Run animation does not apear to work either and he seems to only loop atack animation while following me at a grater speed. Another isue unregarded to this is that i have multiple weapons and when im starting the game im starting with a weapon and is not detecting it as a weapon(i have fists as weapon too) and plays the animations of fists.It only plays the right animation if i change weapon.Is there a way to make the weapon that im starting the game with to be my fists? Also another question is how chould i atach a sound to the animations? Thank you very much!

avatar image RedDevil · Nov 19, 2013 at 12:39 PM 0
Share

forgot to say but the issue with the weapon switching and animation not playing! when im starting the game i get imediately :Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) WeaponSwitching:SelectWeapon(Int32) (at Assets/Scripts/WeaponSwitching.js:80) WeaponSwitching:Awake() (at Assets/Scripts/WeaponSwitching.js:9)

avatar image RedDevil · Nov 19, 2013 at 02:51 PM 0
Share

after further testing i found that the walk animation and run animation do not play because of this error: The animation state could not be played because it couldn't be found! Please attach an animation clip with the name '' or call this function only for existing animations.

avatar image tanoshimi · Nov 19, 2013 at 03:00 PM 0
Share

Is that after you replaced all instances of the code snippets above like suggested?

Show more comments

Follow this Question

Answers Answers and Comments

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

Related Questions

Horror Game AI script recommendation? 1 Answer

Problems with simple AI script 3 Answers

How to make a Script So that my Player Run As Soon as One Hits Play 0 Answers

Animation & Script Help 2 Answers

Enemy Animation Freezing in first frame 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