im trying to get my character to be able to sit down on a key command and on a repeat of that key or on hitting any other movement key have them stand back up. i have 3 animations, 1 sittind down, 1 to be looped for sitting, and 1 for getting back up. even something like WoW would be nice where its animated properly until the character is seated then if the sit key is hit again they stand up or if they are moved they immediatly do whatever action was hit. so basically a script that would, on command, play the sit down animation once then loop the sitting animation and then would either play the stand up animation if the sit key if pressed again or move the character if a different key is hit and go back to the normal standing idle when its done.
A lot depends on your character controller script but to give a starter:
var SitAnimationName : String = "SitDown";
var SittingAnimationName : String = "SittingDown";
private var inTransition : boolean = false;
private var isSitting : boolean = false;
function COSitDown() {
inTransition = true; // to block all actions until player really sits
isSitting = true;
// get the animation state of the sit transition animation
var state : AnimationState = animation[SitAnimationName];
// clamp the animation so we don't get weird artifacts
state.wrapMode = WrapMode.ClampForever;
state.speed = 1.0; // forward
// play the animation
animation.Play(SitAnimationName);
// wait for the duration of the animation
yield new WaitForSeconds(state.length);
// play the sitting animation.
animation.Play(SittingAnimationName);
inTransition = false; // unblock
}
function COStandUp() {
inTransition = true; // again block all actions
var state : AnimationState = animation[SitAnimationName];
state.wrapMode = WrapMode.ClampForever;
// gonna play animation in reverse
state.speed = -1.0;
state.time = state.length;
animation.Play(SitAnimationName);
// wait for duration of animation again
yield new WaitForSeconds(state.length);
state.speed = 1.0; // just for sure, reset play direction to forward
isSitting = false; // we ain't sitting anymore
inTransition = false; // unblock
}
function Update () {
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
if (isSitting) {
// we are sitting, see if we're trying to stand up
// if in transtion don't do anything!
if ((vertical != 0) && (!inTransition)) {
StartCoroutine(COStandUp());
}
return; // don't move while sitting down!
}
if (Input.GetButtonDown("Jump")) {
// we're gonna sit...
StartCoroutine(COSitDown());
return;
}
// do the actual movement after this comment
}