Change head rotation without teleporting

Discussion in 'Plugin Development' started by Rixterz, Jul 23, 2016.

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

    Rixterz

    Hi

    How can you set an entity's head direction without teleporting it? Teleporting is glitchy.
     
  2. Offline

    Zombie_Striker

  3. Offline

    Orange Tabby

    @Rixterz
    I think this works with every living entity but players.
    Code:
    ((CraftLivingEntity) livingEntity).getHandle().yaw = yourYaw;
     
  4. Offline

    Rixterz

    I need it only for players, unfortunately :/

    I've written the following code but the head movement is crazy and the player gets teleported wildly.

    Code:
    Location targetLoc = ent.getLocation();
                 
                    Location playerLoc = p.getLocation();
                    Location eyeLoc = p.getEyeLocation();
                 
                    double tx = targetLoc.getX();
                    double ty = targetLoc.getY();
                    double tz = targetLoc.getZ();
                 
                    double ex = eyeLoc.getX();
                    double ey = eyeLoc.getY();
                    double ez = eyeLoc.getZ();
                 
                    double dx = tx - ex;
                    double dy = ty - ey;
                    double dz = tz - ez;
                    double dxyz = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2) + Math.pow(dz, 2));
                 
                    float currentYaw = eyeLoc.getYaw();
                    float currentPitch = eyeLoc.getPitch();
                 
                    float requiredYaw = (float) (-Math.atan2(dx, dz) * 180 / Math.PI); //(Math.acos(dx / dxz) * 180 / Math.PI);
    
                    float requiredPitch = (float) (-Math.asin(dy / dxyz) * 180 / Math.PI);
                 
                    float yawDiff = requiredYaw - currentYaw;
                    float pitchDiff = requiredPitch - currentPitch;
                 
                    float newYaw = currentYaw + (float) (yawDiff * 0.1);
                    float newPitch = currentPitch + (float) (pitchDiff * 0.1);
                 
                    PacketPlayOutEntityLook packet = new PacketPlayOutEntityLook(p.getEntityId(), toPackedByte(newYaw), toPackedByte(newPitch), ((Entity) p).isOnGround());
                 
                    for(Player online : getServer().getOnlinePlayers())
                    {
                        ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);             
                    }
     
    Last edited: Jul 24, 2016
Thread Status:
Not open for further replies.

Share This Page