- Home /
(Solved) Cannot play animation in Orthello outside start()
The animation (mySprite) does not play in the update function or it's calls. It only plays inside the start function.
a) mySprite is shown as 'OTAnimatingSprite' object in all functions, performed Debug.log(mySprite). b) It's memory intensive, but still tried moving
mySprite = this.GetComponent<OTAnimatingSprite>();
inside the other function in which I do
mySprite.Play("runRight");
This does not work.
My Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public OTAnimatingSprite mySprite;
public static bool isLeft;
public static bool isRight;
public static bool isUp;
public static bool isDown;
/* ============================== CONTROLS ============================== */
// Use this for initialization
void Start ()
{
mySprite = this.GetComponent<OTAnimatingSprite>();
Debug.Log("Player Script Started: " + mySprite);
mySprite.Play("runRight");
}
private void updatePlayerMovement() {
//moveRight();
//moveLeft();
//moveUp();
this.moveRight();
}
private void moveUp() {
this.transform.Translate(Vector3.up * Time.deltaTime*30, Space.World);
}
private void moveDown() {
this.transform.Translate(Vector3.down * Time.deltaTime*30, Space.World);
}
private void moveLeft() {
this.transform.Translate(Vector3.left * Time.deltaTime*30, Space.World);
}
private void moveRight() {
this.transform.Translate(Vector3.right * Time.deltaTime*30, Space.World);
}
public void Update () {
// these are false unless one of keys is pressed
isLeft = false;
isRight = false;
isUp = false;
isDown = false;
// keyboard input
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
isLeft = true;
Debug.Log("Left: " + mySprite);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
isRight = true;
Debug.Log("Right!");
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
isUp = true;
Debug.Log("Up");
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
isDown = true;
Debug.Log("Down!");
}
updatePlayerMovement();
}
}
Answer by mas · Sep 29, 2012 at 12:24 PM
Would be a good thing to provide us with the exact code that does not work because I do not see any mySprite.Play(..) in the Update() function.
however ...
Asuming that the code that does not work in the Update would look a bit like :
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
isRight = true;
mySprite.Play("runRight")
}
I can confirm that this will not work because by calling mySprite.Play you are restarting the animation, each update cycle, while your D or rightArrow key is pressed.
Better would be something like
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
isRight = true;
if (mySprite.animationFrameset!="runRight")
mySprite.Play("runRight")
}
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
How to play an animation in reverse? 1 Answer
Playing an animation when press left mouse key - Using Orthello JavaScript 1 Answer
Two Animations With One Button Press 1 Answer
Spine animation: how to use a skeleton from a project to transfer to a new project in Spine? 1 Answer