ive been looking for the past hour on the net to figure this out and ive given up i need help 
i have this so far
var controller : CharacterController = GetComponent(CharacterController);
if (Input.GetAxis("Jump") && controller.isGrounded)
animation.CrossFade("prejump");
if (!controller.isGrounded && !animation.IsPlaying("prejump"))
animation.CrossFade("jump");
and i want the player to play the jump animation after the prejump animation has finished.
my full code is this if it helps:
function Update () {
if (Input.GetAxis("Vertical") > 0.1 || Input.GetAxis("Vertical") <-0.1)
animation.CrossFade ("walk");
else if (!animation.IsPlaying("atk_use"))
animation.CrossFade ("idle");
if (Input.GetAxis("Horizontal") > 0.1)
animation.CrossFade("walkr");
else if
(Input.GetAxis("Horizontal") < -0.1)
animation.CrossFade("walkl");
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
animation.Play("atk_use");
var controller : CharacterController = GetComponent(CharacterController);
if (Input.GetAxis("Jump") && controller.isGrounded)
animation.CrossFade("prejump");
if (!controller.isGrounded && !animation.IsPlaying("prejump"))
animation.CrossFade("jump");
}
thanks in advance
Could you possibly time the jump animation to play once the prejump has finished like so?:
if (Input.GetAxis("Jump") && controller.isGrounded)
{
animation.Crossfade("prejump");
var length : float = animation.clip.length;
yield WaitForSeconds(length);
if (!controller.isGrounded && !animation.IsPlaying("prejump"))
animation.Crossfade("jump");
}
Just a stab at the dark, I’m at work and dont have a Unity environment handy…
A couple points:
-
“Note that you can’t use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can.” (from http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html)
-
The logic in Update() may need to be reworked a bit. The “if” statements are going to override each other in ways you probably don’t want. For example, say Update() starts the “Jump” animation and the player is pressing forward. In the next Update(), the first “if” statement will be true, so the character will start walking forward in mid-air instead of completing the jump animation. Examine the Third Person Controller script for an example implementation.
-
This is more of an aside: CrossFade() only plays one animation at time, apart from the 0.3 seconds that it fades from the previous animation to the new animation. If you want your character to move diagonally (for example, forward and right at the same time), you’ll need to blend them. But that’s a whole other can of worms (see http://docs.unity3d.com/Documentation/Manual/AnimationScripting40.html – if this link doesn’t work, google “unity animation scripting legacy”).
To address the logic issue, I recommend implementing a simple Finite State Machine (FSM). If you’re unfamiliar with FSMs, it’s a way to break down logic. For example, if the character is in the idle state, you only need to worry about handling what he can do while in the idle state. More info: state driven agent design 1
Here’s a bit of untested code:
enum State { Idle, Walk, Jump, Attack }
enum Direction { Forward, Left, Right }
var currentState : State;
var currentDirection : Direction;
function Update () {
switch (currentState) {
case State.Idle:
Idle();
CheckWalk();
CheckJump();
CheckAttack();
break;
case State.Walk:
Walk();
CheckIdle();
CheckWalk();
CheckJump();
CheckAttack();
break;
case State.Jump:
// Player can't do anything until jump is done.
break;
case State.Attack:
// Player can't do anything until attack is done.
break;
}
}
function CheckIdle() {
// Check if character needs to enter the idle state:
if (Mathf.Abs(Input.GetAxis("Vertical")) < 0.1 && Mathf.Abs(Input.GetAxis("Horizontal")) < 0.1)
Idle();
}
function CheckWalk() {
// Check if character needs to enter a walking state:
if (Input.GetAxis("Vertical") > 0.1 || Input.GetAxis("Vertical") <-0.1)
Walk(Direction.Forward);
else if (Input.GetAxis("Horizontal") < -0.1)
Walk(Direction.Left);
else if (Input.GetAxis("Horizontal") > 0.1))
Walk(Direction.Right);
}
function CheckJump() {
// Check if character needs to enter the jumping state:
if (Input.GetAxis("Jump") && controller.isGrounded)
Jump();
}
function CheckAttack() {
// Check if character needs to enter the attack state:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
Attack();
}
function Idle() {
state = State.Idle;
animation.CrossFade("idle");
}
function Walk(newDirection : Direction) {
state = State.Walk;
direction = newDirection;
switch (direction) {
case Direction.Forward:
animation.CrossFade("walk");
break;
case Direction.Left:
animation.CrossFade("walkl");
break;
case Direction.Right:
animation.CrossFade("walkr");
break;
}
}
function Jump() {
state = State.Jump;
animation.CrossFade("prejump");
yield WaitForSeconds(animation["prejump"].length);
animation.CrossFade("jump");
yield WaitForSeconds(animation["jump"].length);
state = State.Idle;
}
function Attack() {
state = State.Attack
animation.CrossFade("atk_use");
yield WaitForSeconds(animation["atk_use"].length);
state = State.Idle;
}