Cant Flip 2D Sprite

Hi! So I’ve been beating my head against a wall with this all day.
Im trying to get the character sprite to flip when moving the on screen joystick left but everything I’ve tried doesn’t want to work with the way I made the character move

    public float moveForce = 25000;
    Rigidbody2D myRigidbody;

 
   


    void Start()
    {
     
        myRigidbody = this.GetComponent<Rigidbody2D>();
         }

    void FixedUpdate()
    {

        Vector2 horizontal = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * moveForce;

              myRigidbody.AddForce(horizontal);

       
    }

This is what I was trying to use to get the character to flip, but in reality I have no idea what Im doing/doing wrong.

private void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }

Simplify your flip code a lot:

void Flip( float horizontal)
{
  if (horizontal > 0) transform.localScale = new Vector3(  1, 1, 1);
  if (horizontal < 0) transform.localScale = new Vector3( -1, 1, 1);
}

Now… who is calling Flip? I don’t see that in your code anywhere. Is it even being called?

1 Like

Honestly Im not even sure. I really dont know what Im doing and Im just putting together what I can find to piece something together, learning along the way.

 public float moveForce = 25000;
    private Rigidbody2D myRigidbody;
    private Animator myAnimator;
    private bool facingRight;




    void Start()
    {
        facingRight = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        Vector2 Horizontal = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * moveForce;
        Flip(Horizontal);

    }
    void Flip(float Horizontal)
    {
        if (Horizontal > 0) transform.localScale = new Vector3(1, 1, 1);
        if (Horizontal < 0) transform.localScale = new Vector3(-1, 1, 1);
    }
}

Thats what I have the full code as right now, which still gives the same error as before, “cannot convert ‘UnityEngine.Vector2’ expression to type float”

You created a Vector2 which is : Horizontal, Vertical.
Then you tried to pass it to a method accepting a float.

Perhaps you meant to have:

void FixedUpdate() {
   float horz = CrossPlatformInputManager.GetAxis("Horizontal");
   Flip(horz);
  }

That code would work, though the vertical is now missing. Since I’m not sure of the rest of your code, game, or design, I can’t say if that’s okay or not. :slight_smile:
Can the player move up/down, or only side-to-side?

1 Like

Only side to side is what Im attempting

Well try that code snippet in place of what you had in your previous post & see if it works.

So it does get the character to flip but now its lost movement. I’ve gotta go back to work so Ill keep working on it when I get home but thank you very much for your help!

Sure, your second code snippet didn’t have the add force that your earlier post had.
So, add back the add force (or use velocity), and then it should be moving.

:slight_smile:

So it took a minute lol but I got it working thank you! Not the cleanest test but if it does the job Im happy!

using System.Collections;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Mcont : MonoBehaviour
{
    public float moveForce = 25000;
     Rigidbody2D myRigidbody;
  
    void Start()
    {
        myRigidbody = this.GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        float horz = (CrossPlatformInputManager.GetAxis("Horizontal")) * moveForce;
        //bool isBoosting = CrossPlatformInputManager.GetButton("Boost");
        Flip(horz);
        HandleMovement(horz);
    }
    void HandleMovement(float horz)
    {
        myRigidbody.velocity = new Vector2(horz, myRigidbody.velocity.y) * moveForce;
    }

    void Flip(float horz)
    {
        if (horz > 0) transform.localScale = new Vector3(150, 150, 150);
        if (horz < 0) transform.localScale = new Vector3(-150, 150, 150);
    }
}