Hello Guest

Author Topic: Flipping sprite animation for moving other direction.  (Read 10321 times)

FarzanZand

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Flipping sprite animation for moving other direction.
« on: August 12, 2013, 01:43:15 pm »
Hi there!
Figured I would ask for support here instead of the unity forum, just incase someone with the same problem might do a search for flipping. My searches did give some hits, but none that I could personally use because they were above my level.

I have an animation of a sprite for when he walks to the right. What I want is for it to be flipped when I walk to the left, but I can't seem to get it to work. I tried flipX and other recommendations but I am doing them completely wrong since they won't work. Could you explain to me how to flip them for when I do something in reverse direction and then add the line/lines in my code as it should be? I learn best by seeing how it is done and copying it. I have added my code as far as I have gotten here below. I have also tried adding sprite.scale but I must be missing something.

I understand that I can scale the x to -1, but I can't reach it by code. This line doesn't work. :( gameObject.sprite.scale = new Vector3(-1,gameObject.transform.position.y, 0);

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

public class Animations : MonoBehaviour {

//private bool walking = false;

private tk2dSpriteAnimator anim;
// Use this for initialization
void Start () {
anim = GetComponent<tk2dSpriteAnimator>();
}


//Go back to idle when nothing is pressed.
    void HitCompleteDelegate(tk2dSpriteAnimator sprite, tk2dSpriteAnimationClip clip)
{
anim.Play ("idle");
}

// Update is called once per frame
void Update () {

if (Input.GetKey(KeyCode.D))
{
anim.Play("walk");
anim.AnimationCompleted = HitCompleteDelegate;
}

if (Input.GetKey (KeyCode.A))
{
anim.Play("walk"); //I want the sprite play walk flipped. I tried adding sprite.scale *= (-1,1,1); here, but did not work.
anim.AnimationCompleted = HitCompleteDelegate;
}
}
}



Best regards,

Farzan Zand
« Last Edit: August 12, 2013, 03:47:28 pm by FarzanZand »

FarzanZand

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Flipping sprite animation for moving other direction.
« Reply #1 on: August 12, 2013, 04:11:52 pm »
I have tried everything I can think of to flip the sprite. Just tried.


private tk2dBaseSprite xFlip;
xFlip = GetComponent<tk2dBaseSprite>();

xFlip.FlipX(); 

This did not work either. :(

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Flipping sprite animation for moving other direction.
« Reply #2 on: August 12, 2013, 04:47:58 pm »
What are you attaching the script to?

FarzanZand

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Flipping sprite animation for moving other direction.
« Reply #3 on: August 12, 2013, 04:49:42 pm »
To the playerobject, that holds the sprite for the player I am controlling.

I created a sprite with animator, and added the sprite animations that I have created to it, and the animationscript I am using which is attached above.
« Last Edit: August 12, 2013, 05:01:23 pm by FarzanZand »

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: Flipping sprite animation for moving other direction.
« Reply #4 on: August 12, 2013, 04:56:01 pm »
Hi,

You were nearly right with your first attempt - the line

Code: [Select]
anim.Sprite.scale = new Vector3(-1, 1, 1);
before the play line should reverse the sprite.  Alternatively

Code: [Select]
anim.Sprite.FlipX = true;
should also work - note that FlipX is a property, not a function.

Hope this helps.

FarzanZand

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Flipping sprite animation for moving other direction.
« Reply #5 on: August 12, 2013, 05:00:55 pm »
Thank you!!!
It works perfectly now! Oh this is such a relief, and such a silly mistake to be stuck on for five hours.

You just made my day. :)