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 MrTechnomancer · Nov 07, 2012 at 08:05 PM · animationenemyfollowrangerun

Why is my enemy not doing his run animation?

Ok so I'm new to Unity and Scripting. I found this script in a website and I tweeked it a little bit so that my enemy can idle when it's out of range and Run when it's in range and attack when it's in range. so far my enemy idle when out of range but it doesn't do the Run animation when it's following the player. Can someone Rely this with my code fixed? so that it can attack in Range, Run in range and Idle out of range from player? my enemy only has idle, Start (start runing), Run, End (end Running) and Attack.

here is the code i need fixed:

var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var attackThreshold = 3; // distance within which to attack var chaseThreshold = 10; // distance within which to start chasing var giveUpThreshold = 10; // distance beyond which AI gives up var attackRepeatTime = 1; // delay between attacks when within range

private var target : Transform; //the enemy's target private var initialSpeed = 0; private var chasing = false; private var attackTime = 1;

var myTransform : Transform; //current transform data of this enemy

function Awake() { myTransform = transform; //cache transform data for easy access/preformance }

function Start() {

  target = GameObject.FindWithTag("Player").transform; //target the player
   if (!target)
   Debug.Log ("ERROR : player not tagged as 'Player'");
 
 // Set all animations to loop

animation.wrapMode = WrapMode.Loop; // Except our action animations, Dont loop those animation["End"].wrapMode = WrapMode.Once; animation["idle"].wrapMode = WrapMode.Once; animation["Start"].wrapMode = WrapMode.Once;

// Put idle and run in a lower layer. They will only animate if our action animations are not playing animation["idle"].layer = 1; animation["Attack"].layer = 1; animation.Stop(); }

function Update () {

Debug.DrawLine( target.position , myTransform.position , Color.cyan); // check distance to target every frame: var distance = (target.position - myTransform.position).magnitude;

 if (chasing) { 
 

     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.smoothDeltaTime);
   
     

     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.smoothDeltaTime; 
     animation["Run"].layer = 1;

     // give up, if too far away from target:
     if (distance > giveUpThreshold) {
         chasing = false;
         animation["idle"].layer = 1;
         
         
     }

     // attack, if close enough, and if time is OK:
     if (distance < attackThreshold && attackTime == attackRepeatTime) {
         // Attack! (call whatever attack function you like here)
         attackTime = attackTime + attackRepeatTime;
         // Attack Animation
         //animation.CrossFade("attack_animation");
         //animation.Play();
     }

 } else {
     // not currently chasing.

     // start chasing if target comes close enough
     if (distance < chaseThreshold) {
         chasing = true;
         animation["Run"].layer = 1;
       
     }
     if (chasing == true ){

animation.CrossFade("Run"); animation.Play();

} else if (chasing == false) { animation.CrossFade("Start"); animation.Play(); }

 }
 
     

}

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 shadowpuppet · May 08, 2015 at 10:24 PM

found one script that finally works!!!! using UnityEngine; using System.Collections;

public class EnemyBehavior : MonoBehaviour { public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4}; public AnimationClip enemyIdle;//setting animations public AnimationClip enemyWalk;//walking public AnimationClip enemyRun;//running/chasing public AnimationClip enemyAttack;//attacking player public AnimationClip enemyKill;//killing player

  public Transform Player;//adds the player to the script
  
  private int Stopped = 0;
  public int WalkingSpeed = 1;//sets the normal walking speed
  public int RunningSpeed = 5;//sets the running (chase player) speed
  public int DefaultRunningSpeed = 5;
  public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
  public int MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
  public EnemyState currentState;
  
  private bool PlayerDetected;
  
  void Update()
  {
     if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
     {
       PlayerDetected = true;
       if (PlayerDetected)
       {
        print ("Player Detected.");
        transform.LookAt(Player);//Looks at the player
        currentState = EnemyState.RUN;
          transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player
       }
     }else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
     {
       RoamAround ();
     }
     if (Vector3.Distance (transform.position,Player.position) < MinDist)
     {
       currentState = EnemyState.ATTACK;
       Attack ();
  
     }
  
     if (currentState == EnemyState.IDLE)
     {
       //print ("The enemy is idle.");
       animation.CrossFade (enemyIdle.name);
     }
     else if (currentState == EnemyState.WALK)
     {
       print ("The enemy is walking.");
       animation.CrossFade (enemyWalk.name);
     }
     else if (currentState == EnemyState.RUN)
     {
       print ("The enemy is running.");
       print ("Distance: " + Vector3.Distance(transform.position,Player.position));
       animation.CrossFade (enemyRun.name);
       RunningSpeed = DefaultRunningSpeed;
     }
     else if (currentState == EnemyState.ATTACK)
     {
       print ("The enemy is attacking.");
       animation.CrossFade (enemyAttack.name);
       RunningSpeed = Stopped;
     }
     else if (currentState == EnemyState.KILL)
     {;
       print ("The enemy is killing.");
       animation.CrossFade (enemyKill.name);
     }
  
  }
  void RoamAround()
  {
     currentState = EnemyState.IDLE;//Add other RoamAround stuff here
  }
  public void Attack()
  {
     //Add attacking stuff here
     //Application.LoadLevel ("GameOver");
  }

}

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

10 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

Related Questions

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

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

Enemy collider used as range to detect player causes player to not go into jump animation, how should I deal with that? 0 Answers

How do I hurt enemies by jumping on them without getting killed? 1 Answer

How do I make a double-tap system for dashing 7 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