Jump to content

Finding player orientation


Recommended Posts

Posted (edited)

 

This method finds what direction the player is facing based on the previous tile, and assigns it to the 'direction' string.

    private void checkPlayerDirection() {
        int currentX = myPlayer().getX();
        int currentY = myPlayer().getY();
        int deltaX = currentX - previousX;
        int deltaY = currentY - previousY;

        if (deltaX == 0 && deltaY == 0) {
            direction = previousDirection; // Use the previous direction when not moving
        } else if (deltaX == 0) {
            direction = deltaY > 0 ? "North" : "South";
        } else if (deltaY == 0) {
            direction = deltaX > 0 ? "East" : "West";
        } else if (deltaX > 0) {
            direction = deltaY > 0 ? "North-East" : "South-East";
        } else {
            direction = deltaY > 0 ? "North-West" : "South-West";
        }

        if (!direction.equals(previousDirection)) {
            log("Player is facing: " + direction);
            previousDirection = direction;
        }
    }

 

onStart

        previousX = myPlayer().getX();
        previousY = myPlayer().getY();

 

onLoop

	checkPlayerDirection();
        previousX = myPlayer().getX();
        previousY = myPlayer().getY();

 

Variables

    int previousX;
    int previousY;
    String direction;
    String previousDirection;

 

Usage

if(direction == "North") {
        	//do something
        }

Screenshot_4.png

Edited by xlDino
Posted (edited)

You can simply check the rotation of a player, might be easier than checking it like that :D

myPlayer().getRotation()


If you enable myPlayer debug in the debug tab, it will be included. :)
If interacting with an entity these values sometimes shift a little bit.

0 = South
256 = South-West
512 = West
768 = North-West
1024 = North
1280 = North-East
1536 = East
1792 = South-East
HHUflGS.png

Edited by Khaleesi
Posted
2 hours ago, Khaleesi said:

You can simply check the rotation of a player, might be easier than checking it like that :D

myPlayer().getRotation()


If you enable myPlayer debug in the debug tab, it will be included. :)
If interacting with an entity these values sometimes shift a little bit.

0 = South
256 = South-West
512 = West
768 = North-West
1024 = North
1280 = North-East
1536 = East
1792 = South-East
HHUflGS.png

Found this out after hahahahaha

  • Heart 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...