2D side scroller

hello guys , i am new to the world of unity programming , as i am new to programmation , and i was planning to create a small 2D game , but i already have a problem :

so i a am using 2D toolkit pakage alsow ; i have an animated sprit as my main character wich i was aiming to controle (make him slide left and right on the X axis) , with the arrow keys via a script a wrote, the sprit plays a small animation called idle at the start , and i wanted to be able to switch the idle animation with a walking animation as the spriit goes left or rigth… here is the very humble code i managed to produce :

using UnityEngine;
using System.Collections;

public class changeanim : MonoBehaviour {

tk2dSpriteAnimator animator;

public float moveSpeed = 10f;

// Use this for initialization
void Start () {
animator = GetComponent();

}

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

if(Input.GetKey(KeyCode.LeftArrow)){
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
animator.Play (“joewalk”);}

if(Input.GetKey(KeyCode.RightArrow)){
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
transform.Rotate(Vector3.Scale); }

}

}

so with that i am able to displace the main character along the X axis (left and right) and play a walk animation when he goes left , but i have 2 problemes !!

the first is that when the left walk animation(“joewalk”) is played , i get stuck with the walk animation , it dont go back to the idle animation !

and the second problem is that when i go to the left my character is facing left , cause thats how he starts , but i want him to face to the right when i click the rigth arrow and i dont know how to do that

please help me if you know the solution , i am here pulling my hair for 3 hours with that :-[

So I don’t see anything that says to play the idle animation again.

I would set the animations to loop and then run the animation clips like so, in your Update():

if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow)) {
    animator.Play("joewalk");
}

if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow)) {
    animator.Play("joeidle"); // Back to idle animation
}

Now this will probably cause bugs if the player is hitting LeftArrow and RightArrow simultaneously, but it should be enough to get you started.

For making your character face left and right. I would make a function like this:

void UpdateFaceLeftRight() {
    if (Input.GetKey(KeyCode.RightArrow)) {
        transform.localScale = new Vector3(-1, 1, 1); // Flips transform left-to-right
    }
    else {
        transform.localScale = Vector3.one; // Flips back to normal
    }
}

Again, there will be bugs if the player can press LeftArrow and RightArrow simultaneously. Handling that takes a bit more thought.

thank you sooooooooo much garth i really appreciate your help !!! your answer completely solved the problem , i am really happy to see that someone that i absolutely dont know, was fully ready to offer some of his time to help me save up a lot of mine ! since your help i have made a lot of progress ,
and again i am really greatfull that you offered your help ! when i will finish the game , if you accept, i will show it to you so that you see the fruit of your participation , and i dont think there is such thing as a small participation :slight_smile:

You can avoid the bug Garth mentions by seperating the animation logic from the input logic. Here’s what I usually do for character controllers.

declare a global Vector 3 speed variable and edit this in your input code.
i.e.

If (Input.GetKey(KeyCode.RightArow)){
    speed.x = moveSpeed;
}
else if(Input.GetKey(KeyCode.LeftArrow)){
    speed.x = moveSpeed * -1;
}
else{
    speed.x = 0;
}transform.translate(speed*Time.deltatime);

Then play the correct animation based on the value of speed.x like

if(speed.x>0){
    transform.localScale = Vector3.one;
    animation.Play("joewalk");
}
else if(speed.x>0){
    new Vector3(-1, 1, 1);
    animation.Play("joewalk");
}
else{
    animation.Play("joeidle");
}

this way you don’t run into issues with strange input combinations and your character will always have the correct animation for the direction he is moving.

NOTE: to flip your character left and right you can use a built in tk2dSprite function from 2dToolkit instead of modifying the scale of the transform… I can’t think of the exact function off the top of my head.

thanks a lot man the game is on the google play store as of now , and i owe it to you and to all the guys that helped me here ! i hope to have the chance to give back to the community somehow , the name of the game is Z-Fury by the way , if you are curious to see what it migth look like ! and thanks again , gob bless you , i wich you all succes :wink:

thanks a lot man the game is on the google play store as of now , and i owe it to you and to all the guys that helped me here ! i hope to have the chance to give back to the community somehow , the name of the game is Z-Fury by the way , if you are curious to see what it migth look like ! and thanks again , gob bless you , i wich you all succes