How to make a player jump in a certain direction

Discussion in 'Plugin Development' started by TheButlah, Jul 20, 2012.

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

    TheButlah

    Hi, Im working on a plugin that allows a player to launch himself into the air. I have two separate questions:

    I want the player to be launched in the direction they are looking. Additionally, i want the length/height of the jump to vary depending on how high or low the player is looking. For example, if the player looks straight out at the horizon, they will be launched far horizontally (on the X and Z axis) but not as high vertically (Y axis), with the opposite being true if the player looks more at their feet or the sky. The code that I'm currently using is as follows:

    Code:
    public void use(RpgCharacter character) {
            String username = character.getName();
            Player p = plugin.getServer().getPlayer(username);
            //gets the block the player is looking at
            List<Block> line = p.getLineOfSight(null, 15);
            Location loc = line.get(line.size() - 1).getLocation();
         
            //Figures out how many blocks in each direction the player needs to travel
            Vector tempVec = loc.subtract(p.getLocation()).toVector();
            //Sets the magnitude of the horizontal vector to 1, then increases it based on level
            Vector vec = new Vector(tempVec.getX(), 1, tempVec.getZ()).normalize().multiply(level*1.5);
            p.setVelocity(vec);
        }
    I dont really understand vectors or what the normalize() function does or why it really works (I just got lucky after hours of work and discovered it on accident). The problem with this is that the directional stuff doesn't entirely work properly. If, for example, you look straight at you feet or straight up in the air, instead of launching you straight up into the air, it launches you sideways or diagonally into the air.

    So is there a better way to do this? Also is the best way to get the direction the player is looking with getLineOfSight()?

    Edit: level is a number that would determine how far the player gets to jump, i.e. a higher level = farther or higher jump distance
     
  2. normalize on a vector decreases peks on it,, so an Vector(2,1,0) becomes (1,1,0)
     
  3. Offline

    Njol

    Normalizing a vector sets it's length to 1 (keeping it's direction), i.e. (2, 1, 0) becomes (2/sqrt(5), 1/sqrt(5), 0)

    You likely want the player to jump in the direction he is looking which can be done with one line:
    Code:
    player.setVelocity(player.getLocation().getDirection().multiply(level*1.5));[/cpde]
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    TheButlah

    Thanks, you guys are awesome. However, now I've run into a different problem: How do keep the player from taking fall damage but only while they're using the jump skill?
     
  5. Offline

    Phinary

    You can try setting a HashMap or something with the players who have just jumped, then have an event listener for when a player hits the ground, so if they just jumped = true, cancel the damage event and set them jumping to false. I'm sure there is a better way of doing it though.
     
  6. Offline

    TheButlah

    I thought of that, but then what happens if they use the skill and they arent launched high enough to take fall damage? Then the next time they should legitimately take fall damage, they wont.
     
  7. Offline

    Njol

    Try setting the player's fall distance to a high negative value (e.g. -1000000). If that doesn't work try delaying this call by 1 tick using the scheduler.
     
  8. Offline

    TheButlah

    I attempted this, but it appears as though it basically makes the player permanently immune to all fall damage
     
  9. Offline

    r0306

    TheButlah
    If setting fall distance doesn't work, I would say make a toggle for the player everytime they use the skill and remove it after a few seconds through a delayed task. While they have the toggle on, it means that they are jumping and do not receive damage.
     
  10. Offline

    TheButlah

    I thought of that as well. The problem is that since the duration of the jump varies, a specific timed period of immunity to fall damage would not necessarily coincide with the duration of the jump, resulting in a player being able to take advantage of this
     
  11. Offline

    bartboy8

    if(jump.contains(p.getName()){
    player.setFallDistance(-10000000F);
    }else{
    player.setFallDistance(0F);
    }
     
  12. Offline

    TheButlah

    what is jump.contains()?
     
  13. Offline

    bartboy8

    You could make that an ArrayList of strings. to store the players name in the game.
     
  14. Offline

    TheButlah

    I really dont understand how that would solve the problem at all. Could you be more descriptive?
     
Thread Status:
Not open for further replies.

Share This Page