Enemy Animation Play
Hi Everone I have a enemy ai script and it works fine. But when I tried to add the animation code to it, so show me two Error.
(01). Enemy.js(49,26): BCE0019: 'animation' is not a member of 'function(System.Type): UnityEngine.Component'.
(02). Enemy.js(53,30): BCE0019: 'animation' is not a member of 'function(System.Type): UnityEngine.Component'.
My Unity Version 5.4
Here's my script (commented portion is what I put in)
#pragma strict
var chasing : boolean = false;
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float = 10f;
var range2 : float = 10f;
var stop : float = 6f;;
var myTransform : Transform; //current transform data of this enemy
public var Idle : AnimationClip;
public var Run : AnimationClip;
public var Attack : AnimationClip;
public var Die : AnimationClip;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if(moveSpeed < 3 && chasing == true)
{
GetComponent.animation.CrossFade(Idle.name);
}
if(moveSpeed > 3 && chasing == true)
{
GetComponent.animation.CrossFade(Run.name);
}
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
I did the best to my knowledge. PLEASE HELP Thanks
Answer by Cynikal · Nov 03, 2016 at 12:03 PM
GetComponent().CrossFade <--- C#
GetComponent.().CrossFade <--- JS. (Thanks Landern)
Please understand the usage of GetComponent above will return a Component type, use the generic methods ins$$anonymous$$d.
GetComponent<Animation>().CrossFade // C#
GetComponent.< Animation >().CrossFade // UnityScript/JavaScript
Otherwise you will run into the method not found on Component type exceptions.
it works fine. Enemy follow me but animation not play. When the scene starts he's just in T-Pose but when he starts following me he not play the Run animation and it doesn't Play. plz help
Your answer
Follow this Question
Related Questions
Advance AI ( League of Legends ) 0 Answers
Enemy is not taking damage! 0 Answers
My enemy ai won't stay on the ground help please 0 Answers
Idle , Move and Attack animations 0 Answers
How to enemy animation attack player 0 Answers