58
« on: June 25, 2012, 02:47:19 pm »
finally,
i figured out the problem.
first of all, you were right. it is necessary to check if an animation is playing and which one, otherwise at each update it starts again (no animation visible then).
then, by using "A" and "D" as move keys, unity returns true only for the CURRENT FRAME even if the key stays pressed... instead i used horizontal values returned by getAxis.
here's my working code for information :
void Update ()
{
if (Input.GetKeyDown ("space")) { // jump
rigidbody.AddRelativeForce (transform.up * jumpSpeed, ForceMode.Impulse);
}
// if not hiding
if (!hiding) {
movement = Input.GetAxis ("Horizontal") * moveSpeed;
movement *= Time.deltaTime;
transform.Translate (movement, 0.0f, 0.0f);
if (Input.GetKeyDown (KeyCode.S)) { // hide
if (anim.isPlaying () && anim.CurrentClip.name != "hide" || !anim.isPlaying ())
anim.Play ("hide");
hiding = true;
} else if (Input.GetAxis ("Horizontal") > 0) { // walk right
if (anim.isPlaying () && anim.CurrentClip.name != "walk" || !anim.isPlaying ())
anim.Play ("walk");
if (!goingRight)
anim.FlipX ();
goingRight = true;
} else if (Input.GetAxis ("Horizontal") < 0) { // walk left
if (anim.isPlaying () && anim.CurrentClip.name != "walk" || !anim.isPlaying ())
anim.Play ("walk");
if (goingRight)
anim.FlipX ();
goingRight = false;
} else {
if (anim.isPlaying () && anim.CurrentClip.name != "idle" && anim.CurrentClip.name != "unhide" || !anim.isPlaying ()) {
anim.Play ("idle");
}
}
} else {
// unhide
if (Input.GetKeyDown (KeyCode.W)) {
if (anim.isPlaying () && anim.CurrentClip.name != "unhide" || !anim.isPlaying ()) {
anim.Play ("unhide");
}
hiding = false;
}
}
}
thank you for your help, i appreciated your responsiveness !
mike