player.setVelocity() Launches The Player to The Worng Direction

Discussion in 'Plugin Development' started by oriamrm, Dec 31, 2016.

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

    oriamrm

    Hi everyone, so iv'e been using player.setVelocity(player.getLocation().getDirection() for a long time, and I never had any problems with it.

    Now, I see that when I use .multiply(10) it changes the direction, and launches the player to the wrong direction

    Here's My Code:
    Code:
    player.setVelocity(player.getLocation().getDirection().multiply(10).setY(0));
    
    It sends the player to a wrong direction, but still within the 90 degrees he's looking at, what made me think that .getDirection() isn't 100% accurate, and .multiply() enlarges the difference.

    (And sorry for my bad english :))


    EDIT: I *Think* I Found out what the problem is - I am using the player's body's direction, and not his head's. So I tried using player.getEyeLocation() instead, but it still launches the player to the body's direction...

    EDIT #2: Okay, so .getEyeLocation() does work, but the player is just randomly being launched left / right sometimes when I execute this line. I have no idea what's happening :(
     
    Last edited: Dec 31, 2016
  2. Offline

    Asparock

    Hi, I think you have to normalize the vector before multiply, like this :

    Code:
            Vector v = player.getLocation().getDirection();
            v.normalize();
            v.multiply(10);
            player.setVelocity(v);
    
     
  3. Offline

    oriamrm

    Already Tried That... still not working.
     
    Last edited: Jan 2, 2017
  4. Offline

    oriamrm

    Anyone? Still Haven't figured this out...
     
  5. @oriamrm
    Could you do the following:
    1. Set up some code to print out the X and Z Without being multiplied.
    2. Set up some code to print out the X and Z that is being multiplied.
    3. Print out the yaw.
    4. After that, look east a do something to trigger the code above. If you are looking directly east, the yaw should be 0, the X should be some number, and Z should be 0.
    [Edit] If you look in the same direction, does the offset that gets added to the player ever change, or is that offset constant?
     
  6. Offline

    I Al Istannen

    @oriamrm
    To add on to what @Zombie_Striker said:
    • Is there any chance you are colliding with something? Entities count!
    • Does that happen if you look up? Or when you are in the middle of a jump?
     
  7. Offline

    oriamrm

    Okay, I Tried all of these things, and here are the results:

    @Zombie_Striker
    • When I look East, yaw = -90, and z = 6.<Some Numbers>E-17 (I Think That Means It's REALLY close to 0). not sure if you meant I should look South, because if I look south yaw = 0.
    • When I Multiply the vector, everything is working properly: X turns from 1 to 10, and Z turns from ...E-17 to ...E-16.
    • Because of my poor English, I didn't understand what "offset" is... So I searched it and read about it, but I don't understand how it's related to vectors and locations.

    @I Al Istannen
    • No. I am testing this in a completely empty world, and just to be sure I tested it again running "kill @e[type=!Player]" in the background.
    • When I look up, everything works properly. It just launches the player upwards.
    • Same problem happens when I use it while I'm jumping.

    NOTE: I noticed that Vectors has a .clone() void, which means that when I use things like that:
    Code:
    Bukkit.broadcastMessage(ChatColor.YELLOW + "10X: "+ String.valueOf(loc.getDirection().multiply(10).getX()));
    I'm multiplying the original vector. So I am now saving the Vector with a "final" modifier and use .clone() every time I multiply it, just in case. Not sure if it has anything to do with the problem, but I thought maybe it will help us solve this at a later stage.

    Thanks for the reply!
     
  8. Yes, that is what I meant, but it's fine. The numbers still show that all the values are correct.

    Then multiplication is not the problem.

    In this regard, I was referring to the change in direction. The difference between where the player should go and where they actually go is the offset.

    As far as accuracy goes, would have been multiplying the vector by 100 (or even 1,000 if you are multiplying it a third time somewhere else) instead of 10. Although it should not cause the player to change their direction, that would increase there speed, meaning they be going faster and moving further than they should.

    Yesterday, I was helping another member with another problem and it turns out the issue was where they were putting their code. Could you post the event/method used for changing the player's direction?
     
  9. Offline

    oriamrm

    @Zombie_Striker
    Here It Is:
    Code:
    public void dash(Player player){
            final Location loc = player.getLocation();
            Bukkit.broadcastMessage(ChatColor.AQUA + "X: " + String.valueOf(loc.getDirection().getX()));
            Bukkit.broadcastMessage(ChatColor.AQUA + "Z: " + String.valueOf(loc.getDirection().getZ()));
            Bukkit.broadcastMessage(String.valueOf(loc.getYaw()));
            Bukkit.broadcastMessage(ChatColor.YELLOW + "10X: "+ String.valueOf(loc.getDirection().clone().multiply(10).getX()));
            Bukkit.broadcastMessage(ChatColor.YELLOW + "10Z: " + String.valueOf(loc.getDirection().clone().multiply(10).getZ()));
    
            player.setVelocity(loc.getDirection().clone().multiply(10));
          
            new BukkitRunnable(){
                int time = 7;
                public void run(){
                    if(time <= 1){
                        this.cancel();
                    } else {
                        time--;
                    }
                  
                    for(double y = player.getLocation().getY(); y < player.getEyeLocation().getY(); y = y + (player.getEyeLocation().getY() - player.getLocation().getY())/16){
                        player.getWorld().spawnParticle(Particle.SNOWBALL, new Location(player.getWorld(), player.getLocation().getX(), y, player.getLocation().getZ()), 1, 0, 0, 0, 0);
                    }
                    for(Entity entity : player.getNearbyEntities(1, 2, 1)){
                        if(entity instanceof LivingEntity && !(entity instanceof Player)){
                            LivingEntity le = (LivingEntity) entity;
                            le.damage(5, player);
                            le.setVelocity(new Vector(le.getLocation().getX()-player.getLocation().getX(), le.getLocation().getY()-player.getLocation().getY(), le.getLocation().getZ()-player.getLocation().getZ()));
                        }
                    }
                }
            }.runTaskTimer(Bukkit.getPluginManager().getPlugin("TheImmuneSystem"), 0, 1);
          
        }
    and it's being called from this method:
    Code:
    public class InteractSorter implements Listener {
        @EventHandler
        public void sort(PlayerInteractEvent event){
            if(event.getPlayer() == null){
                return;
            }
            Player player = event.getPlayer();
          
    //        if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)){
    //            if(event.getClickedBlock().getType().equals(Material.REDSTONE_BLOCK)){
    //                if(VirusSpawner.spawners.contains(event.getClickedBlock().getLocation().add(0.5, 1, 0.5))){
    //                    VirusSpawner.removeSpawner(event.getClickedBlock().getLocation().add(0.5, 1, 0.5));
    //                }
    //            }
    //        }
            if((!event.getAction().equals(Action.RIGHT_CLICK_AIR) && !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) || event.getHand().equals(EquipmentSlot.OFF_HAND)){
                return;
            }
    
            if(event.getClickedBlock() != null && event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(event.getClickedBlock().getState() instanceof Sign){
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    if(sign.getLine(1).equals(ChatColor.GREEN + "ATM")){
                        //Do Stuff
                    }
                }
            }
    
            //From Here On: Item Checks ONLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            if(event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.AIR)){
                return;
            }
            ItemStack item = event.getPlayer().getInventory().getItemInMainHand();
            if(item.hasItemMeta()){
                if(item.getItemMeta().hasDisplayName()){
                    Weapons weapons = new Weapons();
                    String itemName = ChatColor.stripColor(item.getItemMeta().getDisplayName());
                    switch(itemName){
                    case "Fire Spinner":
                        weapons.flameSpin(player);
                        break;
                    case "Dash":
                        weapons.dash(player);
                        break;
                    case "Shockwave":
                        player.playSound(player.getLocation(), Sound.BLOCK_PISTON_CONTRACT, 1, 1);
                        weapons.shockwave(player, player.getLocation(), 1, false);
                        new BukkitRunnable() { public void run() {    weapons.shockwave(player, player.getLocation(), 2, false);    }}.runTaskLater(Bukkit.getPluginManager().getPlugin("TheImmuneSystem"), 4);
                        break;
                    }
                }
            }
        }
    }
    ^It's called from the very end of the method^
     
  10. Offline

    Whoneedspacee

    Multiplication shouldn't change anything even if it isn't normalized, should still be the same direction albeit further. No idea what the issue is but it probably has something to do with the location itself or a conflicting thing elsewhere.
     
  11. Worng Direction

    Shouldn't just making the number negative make it go in the Right Direction? I'm not extremely good with maths, but I'm pretty sure that's logical.
     
  12. Offline

    kameronn

    @DoggyCode™
    The player is going in the right direction based on his code. player.getDirection() is based on the direction the player is moving not facing.

    He wants to move players in the direction they're facing
     
    DoggyCode™ likes this.
Thread Status:
Not open for further replies.

Share This Page