2D Toolkit Forum
2D Toolkit => Support => Topic started by: FarzanZand 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);
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
-
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. :(
-
What are you attaching the script to?
-
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.
-
Hi,
You were nearly right with your first attempt - the line
anim.Sprite.scale = new Vector3(-1, 1, 1);
before the play line should reverse the sprite. Alternatively
anim.Sprite.FlipX = true;
should also work - note that FlipX is a property, not a function.
Hope this helps.
-
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. :)