Solved Pushing a player forward

Discussion in 'Plugin Development' started by MineCrypt, May 29, 2014.

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

    MineCrypt

    Hey, I have a quick question and hopefully someone can give me a a quick answer. I've been playing around with some things and I'm trying to achieve an idea where if you block with a sword, it will push a player forward. Sorta like a fast sprint or dash. Now I got the easy stuff done, such as the block event and all that simple stuff.

    I'm having troubles with achieving the push forward effect. I tried using setVelocity, but it's unpredictable. That means if a player jumps and blocks, he goes much farther than blocking on the ground. Then I tried to disable the "Dash" when player is in the air. It works for the most part, expect the method I used is messy and result in the effect not working. Since I want this to only be used every so often, I can't risk it just not working because a player jumped and used it half a block from the ground. Also setting the Velocity power too high results in Minecraft moving too fast problems.

    Here's my current little setup:
    Code:
                if (player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) {
     
                    return;
                   
                }
     
                    Vector unitVector = new Vector(player.getLocation().getDirection().getX(), 0, player.getLocation().getDirection().getZ());
                   
                    player.setVelocity(unitVector.multiply(15));
                    
    So hopefully someone can help me either fix Velocity, or find a reliable method of pushing a player forward in the direction of his cursor (Excluding the Y direction.)

    Thanks!
     
  2. Offline

    NathanWolf

    I'm not sure if this will do it, but you want to normalize the vector and I think 15 may be way too high- start with 2, maybe?

    Code:
    Vector unitVector = new Vector(player.getLocation().getDirection().getX(), 0, player.getLocation().getDirection().getZ());
    unitVector = unitVector.normalize();             
    player.setVelocity(unitVector.multiply(2));
     
  3. Offline

    MineCrypt

    Sadly that does not fix my problem and the 15 is just there as a filler :p
     
  4. Offline

    NathanWolf


    Have you tried just doing the velocity push without the block check? I would get it working independently first, it'll be much easier.

    I do something very similar to this, minus zeroing out the y-component. I use a magnitude anywhere from 1.5 to 4 and it seems to work ok in most cases.

    Which part is not working, exactly?
     
  5. Offline

    MineCrypt

    The push works perfectly. The problem is, since I'm setting the Velocity, it will push the player. However, if the player jumps, he will fly much much farther due to no restriction on friction. I tried to disable the jump use, but you can still use it when you are falling because the block beneath you will be considered grass even though you are slightly above ground.
     
  6. Offline

    fearleas

    Do you mean when you jump and than push the player he will fly farther or when he looks up? Need to know than i can maybe help you.
     
  7. Offline

    MineCrypt

    The Y is set to 0. So he will never fly up, but if you jump and right click. Since you are in the air, it will launch you much farther than if you were to block and right click while on the ground.
     
  8. Offline

    NathanWolf

    Oh, I see - hrm. So your problem is basically figuring out if the player is on the ground or not?

    I guess I've never thought of that- have you looked at getFallDistance? I'm not sure if that starts changing when you jump, though.

    I guess one hacky option would be to see how far away from 0 the player's Y location is- meaning if it's close to an even integer, they are likely on the ground. I guess this would break with steps or stairs though.

    I wonder if you could adjust the velocity so that it's correct for when they are in the air- and then when they use the ability, nudge them up in the air a little bit? I'm not sure if that'd work, either.
     
    MineCrypt likes this.
  9. Offline

    fearleas

    Maybe you have to distract the y so he always goes down and i think you already checked that when he is 1 block or higher than the ground it wont work?
     
  10. Offline

    MineCrypt



    After playing around with it, I got something to my liking. I used one of your ideas of nudging them up in the air just a tad. So what happens if I bring them up slightly into the air, then shoot them forward. When they get shot forward they arc slightly upwards so they never touch the ground. It makes it so no matter what, whenever you use the ability to will be slightly in the air. So rather they are in the air or the ground, they get around the same distance when flying forward. Thanks for the suggestion!
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page