I import one model containing three animations. It can automatically play animation when start game.But when I defined its frame in clips. It doesn’t play anymore:( It’s trouble me a lot and I don’t know why… ask for help ~help~
once your animation is split, you need to call the animations manually to make them play.
IE:
meshObject.animation.CrossFade(“animation_name”);
Here is my code. I attached it to an official model. All things perform well as expected.
private var walkSpeed : float = 2.0;
private var gravity = 100.0;
private var moveDirection : Vector3 = Vector3.zero;
private var charController : CharacterController;
function Start()
{
charController = GetComponent(CharacterController);
animation.wrapMode = WrapMode.Loop;
}
function Update ()
{
if(charController.isGrounded == true)
{
if(Input.GetAxis("Vertical") > .1)
{
if(Input.GetButton("Run"))
{
animation.Play("Run");
walkSpeed = 4;
}
else
{
animation.Play("Walk");
animation["Walk"].speed = 1;
walkSpeed = 2;
}
}
else if (Input.GetAxis("Vertical") < -.1)
{
animation["Walk"].speed = -1;
animation.Play("Walk");
walkSpeed = 2;
}
// Create an animation cycle for when the character is turning on the spot
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
{
animation.Play("Walk");
}
// Calculate the movement direction (forward motion)
moveDirection = Vector3(Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
}
Okay first off- don’t post comments as answers.
The problem you’re having is because you’re playing your animation in update. Every time you call animation. Play it starts that animation over at the first frame- and Update is called some 30-300 times per second. So yeah, it’s never going to play because you keep restarting it.
You need to use a Finite State Machine or some other way of controlling what ‘state’ your character is in. In other words- is he walking? running? attacking? flying? jumping? taking damage? any possible state needs to be accounted for.
Afterward, you simply test to see if the character is in a new state- and play the new animation. Here’s an example that you can work from:
void Update()
{
ProcessCurrentState();
}
void ProcessCurrentState()
{
switch(CurrentState)
{
case EnemyState.Attack:
Attack();
break;
case EnemyState.Aggro:
Aggro();
break;
case EnemyState.Pursuit:
Pursue();
break;
case EnemyState.Evade:
Evade();
break;
}
}
public void BeginAggro()
{
lastPosition = myTransform.position;
lastRotation = myTransform.rotation;
aggroTrigger.radius = PursuitRadius;
meshRef.animation.CrossFade("attack1");
StartCoroutine("WaitForAggroAnimation");
}
IEnumerator WaitForAggroAnimation()
{
yield return new WaitForSeconds(1);
CurrentState = EnemyState.Aggro;
}
void Aggro()
{
// aggro setup stuff would go here.
BeginPursuit();
}
In this example, the character’s state is changed by external motivation- for example, when the player enters a trigger- the trigger calls Enemy.BeginAggro() and the loop begins.
Hope that helps.