- Home /
Question by
importguru88 · Aug 03, 2016 at 07:39 PM ·
animationscript.animator controllerplayenemy ai
How do I use the same script on enemy ai's and play animations in the animator controller properly on unity3d
I having a problem . I have two of the same enemy ai . I am not spawning then yet . I just test out my script . I only got one enemy ai to play it animation. The other enemy ai only stays on one animation and doesn't play the rest of the animation in the scene. I don't know what is going on . Here is the script I am using :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform player;
static Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
}
public void Stop(){
anim.SetBool("isMoving", false);
anim.SetBool("isAttack", false);
anim.SetBool("isIdle", true);
this.enabled = false;
//Or if you want to destroy the AI script completely
//Destroy(this)
}
void Update ()
{
float speed = 0.1f;
this.transform.Translate(0,0,speed * Time.deltaTime);
if (Vector3.Distance(player.position, this.transform.position) < 25f)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
this.transform.Translate(0,0,0.09f);
anim.SetBool("isMoving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("isMoving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("isMoving",false);
anim.SetBool("isAttack",false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Animation created via script does not play 1 Answer
Trying to learn Java Script but having some issues...with this code. 0 Answers
How to move humanoid avatar when input is word 0 Answers
How do i use the animator avatar mask to combine two animations ? 0 Answers
cant trigger animation while jump 0 Answers