Moving player forward

Discussion in 'Plugin Development' started by Freack100, Apr 18, 2014.

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

    Freack100

    Yes, I know. That sounds kinda stupid xD
    All I want is to make a minigame and the players ALLWAYS have to move in the direction they look.
    I tried player.getLocation().getDirection() and player.setVelocity() but then, the player is moved up too. Then I tried calculating x,y and z myself from the pitch and yaw. that doesn't work too.

    How can I get the direction a player is looking at, but as he/she would look with no horizontal movement?
     
  2. Offline

    TheMcScavenger

    Use the onPlayerMoveEvent. Get their original x,y,z and compare it to their new x,y,z. Then get the direction they were looking at, check whether the increase/decrease in x,y,z corresponds with the direction they were looking at. If not, cancel the event.
     
  3. Offline

    Freack100

    TheMcScavenger
    Thanks, but that's not really what I want...
    I have e scheduler for testing purposes:
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    2.  
    3. public void run(){
    4. for(Player player : Bukkit.getOnlinePlayers()){
    5. double yaw = 0;//((player.getLocation().getYaw())*Math.PI) / 180;
    6. double pitch = (player.getLocation().getPitch()+90) * (180 / Math.PI);
    7.  
    8. double x = Math.sin(pitch) * Math.cos(yaw);
    9. double y = Math.sin(pitch) * Math.sin(yaw);
    10. double z = Math.cos(pitch);
    11.  
    12. Vector vel = new Vector(x, y, z);
    13.  
    14. player.setVelocity(vel);
    15. }
    16. }
    17.  
    18. }, 10L, 10L);
    19. }


    I know that the math for pitch and yaw is wrong, but I hope you get what I want now ^^
     
  4. Offline

    TheMcScavenger

    Freack100 I found the following code after a bit of searching, that allows the player to move towards a specific direction...

    Code:
    Location location = [any location you want here]
    Int speed = [any number]
    Vector dir = location.toVector().subtract(player.getLocation().toVector()).normalize();
    player.setVelocity(dir.multiply(speed));
    How about if you put in a PlayerMoveEvent, get the location they're looking in, set the location to one block forward in the direction they're looking at, move them there, then execute the code again if they've reached that block (which would once again check their location, and could move them in another direction).

    All it would require is moving the player 0.1 forward when you want the code to start running.
     
  5. Offline

    Freack100

    TheMcScavenger
    Thanks for your help but I fixed it myself,
    here is the scheduler I'm using now:
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    2.  
    3. public void run(){
    4. for(Player player : Bukkit.getOnlinePlayers()){
    5.  
    6. if(player.isInsideVehicle()){
    7.  
    8. Location loc = player.getLocation();
    9. loc.setPitch(0);
    10.  
    11. Vector vel = loc.getDirection();
    12.  
    13. player.getVehicle().setVelocity(vel.multiply(2));
    14. }
    15.  
    16. }
    17. }
    18.  
    19. }, 10L, 10L);
    20. }
     
Thread Status:
Not open for further replies.

Share This Page