[Solved] Always Left with TouchDown Click

Hey guys… Im having a little trouble with this… You see, i use this to check if the player clicked to the left side of the player or the right side . If he clicks at the right side of him, he is supposed to shoot to the right… For some reason, Its not working anymore,it only says “LEFT,LEFT,LEFT”. Does anyone see a problem with this code that i dont?

Thanks for reading even if you cant help :stuck_out_tongue:

By the way, this method below is in class :

 public class Player extends Sprite implements InputProcessor  
@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        if (screenX > getX()) {
            System.out.println("Right");
            archerShooting.startAnimation(archerShooting.DIRECTION_RIGHT);
            archerShooting.resetAnimation();

            shooting = true;
        } else if (screenX < getX()) {
            System.out.println("LEFT");
            archerShooting.startAnimation(archerShooting.DIRECTION_LEFT);
            archerShooting.resetAnimation();

            shooting = true;
        }

        return true;
    }

Hi,

(I suppose this is the problem)
you have to convert the mouse coordinates to world coordinates:


Vector3 touch = new Vector3(screenX, screenY, 0);
camera.unproject(touch); // Your OrthographicCamera
Vector2 vec2Touch = new Vector2(touch.x, touch.y);

then you can check on which side of your player your mouse coordinates are


if(vec2Touch.x > getX()) {
...

Combine above answer with little optimization,


    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector2.tmp1.set(screenX, screenY);
        camera.unproject(Vector2.tmp1);

        if (Vector2.tmp1.x > getX()) {
            System.out.println("Right");
            archerShooting.startAnimation(archerShooting.DIRECTION_RIGHT);
        } else {
            System.out.println("LEFT");
            archerShooting.startAnimation(archerShooting.DIRECTION_LEFT);
        }
        
        archerShooting.resetAnimation();
        shooting = true;

        return true;
    }

Thanks guys, it was indeed a camera issue. Solved!

:stuck_out_tongue: