Look at Block

Discussion in 'Plugin Development' started by itunes89, Aug 21, 2013.

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

    itunes89

    Hey guys. I have a plugin I'm making and I need all players when teleported to look in the direction of that block. How would I do this, I know Location has a constructor for pitch and yaw.

    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  2. Offline

    aredherring

    Hold on, I'll look it up now, I just need to find the API :) Probably pretty simple
    OK so I'm going to assume you have the block you want to look at.

    EDIT: Nvm, it's 2am so my judgement is derpy. Basically you're looking at changing Yaw. Sorry I can't be of more help, I'll try and give you code when I am fully awake

    Yaw is looking left-right, Pitch is up-down.
     
  3. Offline

    Jumla

    The getTargetBlock method would work well for this...
    Stupid Jumla, read the question wrong. Give me a sec, I'll help you out.

    That should do it. Not the prettiest code, but it does the job.
    Code:
    private void lookTo(Player p, float x, float y, float z) {
            x = (float) p.getLocation().getX() - x;
            y = (float) p.getLocation().getY() - y;
            z = (float) p.getLocation().getZ() - z;
            final float f1 = (float) Math.sqrt(x * x + z * z);
            float f2 = (float) Math.acos(x / f1);
            if (z < 0.0F) {
                f2 *= -1.0F;
                f2 += 6.283186F;
            }
            final float f3 = (float) Math.atan(y / f1);
            final Location l = p.getLocation();
            l.setPitch(f3 * 180.0F / 3.14159F);
            l.setYaw(f2 * 180.0F / 3.14159F + 90.0F);
            p.teleport(l);
        }
    Good luck with your hunger games plugin!
     
  4. Offline

    itunes89

    Thanks guys.
     
Thread Status:
Not open for further replies.

Share This Page