1
Support / How to get the player to go straight to idle after letting go of the button?
« on: April 06, 2013, 07:10:18 pm »
Here the general code from documents.
using UnityEngine;
using System.Collections;
public class TutorialAnimController : MonoBehaviour
{
virtual public float x{get; protected set;}
/// <summary>
/// The y movement. 1.0f = up, -1.0f = down.
/// </summary>
virtual public float y{get; protected set;}
/// <summary>
/// Is the jump button being held down.
/// </summary>
virtual public bool jumpButtonHeld{get; protected set;}
/// <summary>
/// Was the jump button pressed this frame.
/// </summary>
virtual public bool jumpButtonDown{get; protected set;}
// Link to the animated sprite
private tk2dAnimatedSprite anim;
// State variable to see if the character is walking.
private bool walking = false;
// Use this for initialization
void Start () {
// This script must be attached to the sprite to work.
anim = GetComponent<tk2dAnimatedSprite>();
}
// This is called once the hit animation has compelted playing
// It returns to playing whatever animation was active before hit
// was playing.
void WalkCompleteDelegate(tk2dAnimatedSprite sprite, int clipId) {
if (walking) {
anim.Play("walk");
}
else {
anim.Play("idle");
}
}
// Update is called once per frame
void Update () {
// jumpButtonHeld = false;
// jumpButtonDown = false;
x = 0;
y = 0;
if (Input.GetKey(KeyCode.Space)) {
// Only play the clip if it is not already playing.
// Calling play will restart the clip if it is already playing.
if (!anim.IsPlaying("jump")) {
anim.Play("jump");
// The delegate is used here to return to the previously
// playing clip after the "hit" animation is done playing.
// anim.animationCompleteDelegate = HitCompleteDelegate;
}
}
if (Input.GetKey(KeyCode.D)) {
if (!anim.IsPlaying("walk")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walk");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
if (Input.GetKey(KeyCode.A)) {
if (!anim.IsPlaying("walk-l")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walk-l");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
if (Input.GetKey(KeyCode.W)) {
if (!anim.IsPlaying("idle")) {
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
}
}
using UnityEngine;
using System.Collections;
public class TutorialAnimController : MonoBehaviour
{
virtual public float x{get; protected set;}
/// <summary>
/// The y movement. 1.0f = up, -1.0f = down.
/// </summary>
virtual public float y{get; protected set;}
/// <summary>
/// Is the jump button being held down.
/// </summary>
virtual public bool jumpButtonHeld{get; protected set;}
/// <summary>
/// Was the jump button pressed this frame.
/// </summary>
virtual public bool jumpButtonDown{get; protected set;}
// Link to the animated sprite
private tk2dAnimatedSprite anim;
// State variable to see if the character is walking.
private bool walking = false;
// Use this for initialization
void Start () {
// This script must be attached to the sprite to work.
anim = GetComponent<tk2dAnimatedSprite>();
}
// This is called once the hit animation has compelted playing
// It returns to playing whatever animation was active before hit
// was playing.
void WalkCompleteDelegate(tk2dAnimatedSprite sprite, int clipId) {
if (walking) {
anim.Play("walk");
}
else {
anim.Play("idle");
}
}
// Update is called once per frame
void Update () {
// jumpButtonHeld = false;
// jumpButtonDown = false;
x = 0;
y = 0;
if (Input.GetKey(KeyCode.Space)) {
// Only play the clip if it is not already playing.
// Calling play will restart the clip if it is already playing.
if (!anim.IsPlaying("jump")) {
anim.Play("jump");
// The delegate is used here to return to the previously
// playing clip after the "hit" animation is done playing.
// anim.animationCompleteDelegate = HitCompleteDelegate;
}
}
if (Input.GetKey(KeyCode.D)) {
if (!anim.IsPlaying("walk")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walk");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
if (Input.GetKey(KeyCode.A)) {
if (!anim.IsPlaying("walk-l")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walk-l");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
if (Input.GetKey(KeyCode.W)) {
if (!anim.IsPlaying("idle")) {
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
}
}