Playermovement

Discussion in 'Plugin Development' started by TidalRyan, Aug 23, 2016.

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

    TidalRyan

    How do I cancel the players movement, and then make them move in a different random direction.
     
  2. @TidalRyan
    Listen to the PlayerMoveEvent, and instead of cancelling it, use the ".setTo()" direction, putting in a location with a random direction.
     
  3. Offline

    TidalRyan

    How?

    importorg.bukkit.Bukkit;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.EventPriority;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerMoveEvent;import org.bukkit.util.Vector;
    public class playerMove implements Listener {

    public playerMove(){
    Bukkit.getPluginManager().registerEvents(this, Spellbook.getInstance());}

    @EventHandler(priority = EventPriority.MONITOR)

    public void playerMoveE(PlayerMoveEvent event) {

    Player player = event.getPlayer();
    //Inverts it, but how do I make it random.
    player.setVelocity(new Vector(player.getVelocity().getX() * - 1, player.getVelocity().getY(), player.getVelocity().getZ() * - 1 ));


    }

    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. @TidalRyan
    Use the Random class to make some random numbers, and set the players velocity to those.
     
  5. Offline

    Zombie_Striker

    @AlvinB
    The problem with that, is that the movement is truly random. This means there is a possibility that the player try to move, but the player will return back to the same spot. Since move event can be called multiple times a second, that means the player can change direction multiple times per second. If you want them to have slightly random movement, create a vector which will represent an X and Z movement for a player for one second. Do this by using a Repeating task, and change the XZ value every time it runs. Every time they move, set their velocity equal to the XZ value. This will mean the player will move in a random direction, but it will be semi-constant.
     
Thread Status:
Not open for further replies.

Share This Page