Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Get camera behind player?


    JAG98
     Share

    Recommended Posts

    With scripts involving a lot of player movement, many times the player ends up at the edges of the screen. What camera methods can be used to get the camera behind player character?
    Camera.rotateToEntity() only seems to bring the entity in focus

    Link to comment
    Share on other sites

    So you will want to find the current direction of localPlayer, and then use that to calculate the yaw required to be behind the player.

     

    @Neffarion dropped this pearl of Wisdom in discord a while back, think its exactly what you need:

     

    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.input.Camera;
    import org.dreambot.api.wrappers.interactive.GameObject;
    
    import java.util.Arrays;
    import java.util.Random;
    
    public enum CameraOrientation {
    
        NORTH(2, 0),
        SOUTH(8, 1000),
        EAST(4, 1500),
        WEST(1, 500);
    
        private final int orientation;
        private final int yaw;
    
        CameraOrientation(int orientation, int yaw) {
            this.orientation = orientation;
            this.yaw = yaw;
        }
    
        public static CameraOrientation fromGameObject(GameObject o) {
            return Arrays.stream(values()).filter(i -> i.getOrientation() == o.getOrientation()).findFirst().orElse(null);
        }
    
        public static CameraOrientation getCurrent() {
            int yaw = Camera.getYaw();
            
            if (yaw < 1650 && yaw >= 1250) {
                return EAST;
            }
    
            if (yaw < 1250 && yaw > 760) {
                return SOUTH;
            }
    
            if (yaw < 350 || yaw >= 1650) {
                return NORTH;
            }
    
            return WEST;
        }
    
        public int getOrientation() {
            return orientation;
        }
    
        public int getYaw() {
            return yaw;
        }
    
        public CameraOrientation getOpposite() {
            if (this == NORTH) {
                return SOUTH;
            }
    
            if (this == SOUTH) {
                return NORTH;
            }
    
            if (this == EAST) {
                return WEST;
            }
    
            return EAST;
        }
    
        public void rotateCamera() {
            int yaw = getYaw();
            int rng = (Calculations.random(0, 200) * (new Random().nextBoolean() ? 1 : -1));
            if (this != NORTH) {
                yaw += rng;
            } else {
                yaw += Math.abs(rng);
            }
    
    
            Camera.mouseRotateTo(yaw, Camera.getPitch());
        }
    
    }

     

    Link to comment
    Share on other sites

    8 hours ago, TheCloakdOne said:

    So you will want to find the current direction of localPlayer, and then use that to calculate the yaw required to be behind the player.

    Based on this and the snipped of code shared, does this work?

    Spoiler
    
    private void cameraSetter(){
        if (Camera.getPitch() < 250){
            Camera.keyboardRotateToPitch(Calculations.random(250, 300));
        }
    
        Direction playerFacing = getLocalPlayer().getFacingDirection();
    
        switch ((playerFacing)){
            case EAST:
                if(Camera.getYaw()>=1650 || Camera.getYaw()<1250){
                    Camera.keyboardRotateToYaw(Calculations.random(1250,1650));
                }
                break;
    
            case SOUTH:
                if(Camera.getYaw()>=1250 || Camera.getYaw()<=760){
                    Camera.keyboardRotateToYaw(Calculations.random(760,1250));
                }
                break;
    
            case NORTH:
                if(Camera.getYaw()>=350 || Camera.getYaw()<1650){
                    Camera.keyboardRotateToYaw(Calculations.random(350,1650));
                }
                break;
    
            case WEST:
                if(Camera.getYaw()<350 || Camera.getYaw()>=760){
                    Camera.keyboardRotateToYaw(Calculations.random(350,760));
                }
                break;
                
            case NULL:
                break;
        }
    }

    ETA: seems to work. Doesn't always get the camera behind player but the player no longer resides at the edges of the screen. Also doesn't seem to work when the player is animating like running or casting a spell.

    Edited by JAG98
    Link to comment
    Share on other sites

    19 hours ago, JAG98 said:

    Based on this and the snipped of code shared, does this work?

      Reveal hidden contents
    
    
    
    
    private void cameraSetter(){
        if (Camera.getPitch() < 250){
            Camera.keyboardRotateToPitch(Calculations.random(250, 300));
        }
    
        Direction playerFacing = getLocalPlayer().getFacingDirection();
    
        switch ((playerFacing)){
            case EAST:
                if(Camera.getYaw()>=1650 || Camera.getYaw()<1250){
                    Camera.keyboardRotateToYaw(Calculations.random(1250,1650));
                }
                break;
    
            case SOUTH:
                if(Camera.getYaw()>=1250 || Camera.getYaw()<=760){
                    Camera.keyboardRotateToYaw(Calculations.random(760,1250));
                }
                break;
    
            case NORTH:
                if(Camera.getYaw()>=350 || Camera.getYaw()<1650){
                    Camera.keyboardRotateToYaw(Calculations.random(350,1650));
                }
                break;
    
            case WEST:
                if(Camera.getYaw()<350 || Camera.getYaw()>=760){
                    Camera.keyboardRotateToYaw(Calculations.random(350,760));
                }
                break;
                
            case NULL:
                break;
        }
    }

    ETA: seems to work. Doesn't always get the camera behind player but the player no longer resides at the edges of the screen. Also doesn't seem to work when the player is animating like running or casting a spell.

    When you are running your character can be in a diagonal facing direction, which getFacingDirection() returns NULL
    You have to use the player orientation and convert it

    The snippet above that I had made was only for game objects since they only have 4 orientations. But players can be facing diagonals so you have 4 more orientations (north-east, north-west, south-east and south-west). Plus the values are different for gameobjects and players
     

    You can make something similar to the snippet above and include those 4 missing if you want. Use the code below to get a clear value of your player direction, including diagonals

    int facingDirection = Players.localPlayer().getOrientation() >> 8

     

    Edited by Neffarion
    typo
    Link to comment
    Share on other sites

    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
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.