Hello Guest

Author Topic: Sprite Animation Issue  (Read 4002 times)

weinster

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite Animation Issue
« on: February 01, 2013, 02:06:43 pm »
Hi,

I have read through the documentation and searched around before i posted this but have yet to find an answer that works for me.  I am having an issue with getting the my 2d Toolkit animation sprite to work properly.  It seems that i can either move my character and have the animations stick on the first frame, or my character does not move but all the animations run just fine.  Here is an abbreviated section of my code...

This one will play the animations just fine but not move the character -

Code: [Select]
using UnityEngine;
using System.Collections;

public class AnimControllerTest : MonoBehaviour {

public float runSpeed;
public float gravity;
Vector2 velocity = Vector2.zero;

tk2dAnimatedSprite anim;

void Start ()
       {
anim = GetComponent<tk2dAnimatedSprite>();
        }

void Update ()
       {

CharacterController player = GetComponent < CharacterController > ();

if (player.isGrounded)
{
velocity = new Vector2 (Input.GetAxis ("Horizontal"),0);
WalkAndRun();
}

velocity.y -= gravity * Time.deltaTime;
player.Move (velocity * Time.deltaTime);

}

void WalkAndRun ()
{
if (Input.GetKeyUp (KeyCode.RightArrow))
{
anim.Play("Idle Right");
                anim.animationCompleteDelegate = null;
}
if (Input.GetKeyUp (KeyCode.LeftArrow))
{
anim.Play("Idle Left");
                anim.animationCompleteDelegate = null;
}
if (Input.GetKeyDown (KeyCode.RightArrow))
{
velocity.x *= runSpeed;
anim.Play("Run Right");
                anim.animationCompleteDelegate = null;
}
if (Input.GetKeyDown (KeyCode.LeftArrow))
{
velocity.x *= runSpeed;
anim.Play("Run Left");
                anim.animationCompleteDelegate = null;
}
}
}

This one will move the character but the animation sticks on the first frame -

Code: [Select]
using UnityEngine;
using System.Collections;

public class AnimControllerTest : MonoBehaviour {

public float runSpeed;
public float gravity;
public int lookDirection = 1;
Vector2 velocity = Vector2.zero;

tk2dAnimatedSprite anim;

void Start ()
{
anim = GetComponent<tk2dAnimatedSprite>();
}

void Update ()
{

CharacterController player = GetComponent < CharacterController > ();

if (player.isGrounded)
{
velocity = new Vector2 (Input.GetAxis ("Horizontal"),0);
WalkAndRun();
WhereAmILooking();

}

velocity.y -= gravity * Time.deltaTime;
player.Move (velocity * Time.deltaTime);

}

void WalkAndRun ()
{
if (velocity.x == 0 && lookDirection == 1)
{
anim.Play("Idle Right");
                anim.animationCompleteDelegate = null;
}
if (velocity.x == 0 && lookDirection == 0)
{
anim.Play("Idle Left");
                anim.animationCompleteDelegate = null;
}
if (velocity.x > 0  && lookDirection == 1)
{
velocity *= runSpeed;
anim.Play("Run Right");
                anim.animationCompleteDelegate = null;
}
if (velocity.x < 0  && lookDirection == 0)
{
velocity *= runSpeed;
anim.Play("Run Left");
                anim.animationCompleteDelegate = null;
}
}

void WhereAmILooking ()
{

if (velocity.x > 0)
{
lookDirection = 1;
}
if (velocity.x < 0)
{
lookDirection = 0;
}
}
}

Im fairly new to all this and would appreciate any help in letting me know where im going wrong.

Thanks,

Ross.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Animation Issue
« Reply #1 on: February 01, 2013, 02:23:30 pm »
The reason the animation is stuck on the first frame is that the Play function will restart the animation if you call it on a clip which is already playing.

You can do something like this:
if (!anim.IsPlaying("Idle Left")) anim.Play("Idle Left");

This won't restart the animation every frame, and you should see it play through.

weinster

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite Animation Issue
« Reply #2 on: February 01, 2013, 08:25:49 pm »
Thanks unikron for replying but i have another issue.  I included the code that you suggested and unity is now reporting an error.  It says 'tk2dAnimatedSprite' does not contain a definition for 'IsPlaying'.  There is an IsInvoking that comes up and i tried using that but i get the same result of the animation constantly resetting and my character is locked in the first frame.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Animation Issue
« Reply #3 on: February 01, 2013, 08:32:14 pm »
What version are you on? This particular function (IsPlaying(clipName)) was only added in 1.90

weinster

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite Animation Issue
« Reply #4 on: February 01, 2013, 09:18:09 pm »
Thank you unikron!  Once i updated to 1.90 it all worked perfectly.