Solved Getting a block to a player's right/left

Discussion in 'Plugin Development' started by AGuyWhoSkis, Oct 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    AGuyWhoSkis

    Hey guys! I need help with getting a block which is 10 blocks to a players left and right. Here's the catch: it has to be the 90 degrees to their left/right, not just the adjacent block. Here is a diagram in case you don't understand: http://sketchtoy.com/51420594
     
  2. AGuyWhoSkis
    You can do something like this:

    Code:java
    1. player.getLocation().getBlockX() + 1; //change the number to your liking


    I've never tested that before, but I think it'll work :)
     
    Tehmaker likes this.
  3. Offline

    Tehmaker



    If you want it to be left and/or right of the player, you would have to get the direction he is looking at?
     
  4. Offline

    chasechocolate

    The Gaming Grunts that would always be the same, and the OP wants it to vary depending on the direction the player is looking. You would need to use the players yaw.
     
    AGuyWhoSkis likes this.
  5. Eh, I knew it'd be something along those lines :p
     
  6. Offline

    Tirelessly

    AGuyWhoSkis

    Try this:
    Code:
    final float newZ = (float)(player.getLocation().getZ() + ( amt * Math.sin(Math.toRadians(player.getLocation().getYaw() + 90 * direction))));
     
    final float newX = (float)(player.getLocation().getX() + ( amt * Math.cos(Math.toRadians(player.getLocation().getYaw() + 90 * direction))));
    
    Where: player is your Player object, amt is the distance, and direction is either 1 (front/back) or 0 (left/right). In your case you'd want direction to be 0.

    To get the block to the right, have distance as 10, to get the block to the left, -10

    EDIT/note: This is from my game, so it may not work. That's also why there's extra variables such as direction.
     
    Float213, Goblom and AGuyWhoSkis like this.
  7. Offline

    blablubbabc

    Or try this:

    Code:java
    1.  
    2. public static Vector getRightHeadDirection(Player player) {
    3. Vector direction = player.getLocation().getDirection().normalize();
    4. return new Vector(-direction.getZ(), 0.0, direction.getX()).normalize();
    5. }
    6.  
    7. public static Vector getLeftHeadDirection(Player player) {
    8. Vector direction = player.getLocation().getDirection().normalize();
    9. return new Vector(direction.getZ(), 0.0, -direction.getX()).normalize();
    10. }
    11.  


    These will each return a vector of length 1 in the wanted direction. To get a block 10 blocks far from the player in that direction, you could do something like this:

    Block rightBlock = player.getEyeLocation().add(getRightHeadDirection(player).multiply(10.0D)).getBlock();
     
    wellnesscookie likes this.
  8. Offline

    AGuyWhoSkis

    This works perfectly. Thanks so much!
     
    Tirelessly likes this.
Thread Status:
Not open for further replies.

Share This Page