Check if player is walking backwards

Discussion in 'Plugin Development' started by sniddunc, Aug 16, 2016.

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

    sniddunc

    Hello,

    I am currently making a plugin which has a feature where a player cannot walk over a line of redstone.
    Currently, if they walk into a line of redstone, it throws them backwards. However, if they back up into redstone is sends them flying over the redstone. I am needing to find a way to make sure they can't back over it to exploit this system.

    I spent a long time trying to figure out how to compare the direction a player is facing relative to the direction they're moving but I haven't had any luck with this and am wondering if anyone could help.

    For example, if a player is moving forward I want to check if they're actually looking forward. I suppose a better way to put it would be I want to check if the player is walking backwards or not.

    Any help with this would be immensely appreciated.

    Thank you!
     
  2. Offline

    Zombie_Striker

    @sniddunc
    That is because you are getting where they came from simply bases on on where they are facing. Simply reverse their velocity by inverting the X/Z values for "Player#getVelocity"
     
  3. Offline

    sniddunc

    It didn't work, it just makes them walk slower across the redstone line rather than throwing them back. Here is the bit of code I'm using for this:

    PHP:
    Vector vel player.getVelocity();
    vel.setX(vel.getX() * -1);
    vel.setZ(vel.getZ() * -1);
    player.setVelocity(vel);
    Am I reversing the X and Z wrong?

    I have found multiple ways to shoot the player backwards if they are facing the block they're moving into, and also ways to shoot the player away from the block if they're facing away from it.

    Moving into the block while facing it shoots them away from the block, but moving into the block while facing away from it shoots them OVER the block. I can't figure out how to get it to shoot them away regardless of the way they're facing.

    If anyone could offer any more help with this, then it would be greatly appreciated. This has been an obstacle in my plugin for a while now and I've spent well over 6 hours trying to fix it to no avail. This is driving me crazy.
     
    Last edited: Aug 18, 2016
  4. Offline

    Zombie_Striker

    @sniddunc
    That is fine, though you if you want to shoot them backwards, you are going to need to increase the velocity (Multiplying their velocity by three should be enough.)

    [Edit] you are forgetting that this may invert your inverted velocity. Make sure this invert will only happen once every couple of ticks (lets say 5 ticks).
     
  5. Offline

    sniddunc

    I created an event to make sure it only happens every 6 ticks, but it still sends them over the line if they walk backwards into it.

    This is what I'm using:

    PHP:
    if (!repulsed.contains(player.getName())) {
        
    player.setVelocity(event.getTo().getDirection().multiply(-2));

        
    SaltRepulseEvent re = new SaltRepulseEvent(player5);
        
    re.setTask(Bukkit.getScheduler().runTaskTimer(pluginre0L1L));
    }
    The event handles the adding/removal of names to the repulsed ArrayList. SaltRepulseEvent's variables are the player and the ticks it has to wait before repulsing again.

    Here's a gif of the problem: https://gyazo.com/1f3353ba7ff7fc3d987c9dd74e901807
     
    Last edited: Aug 18, 2016
  6. Offline

    Zombie_Striker

    @sniddunc
    getDirection is not the same as the player's Velocity!!!. The direction is where the player is looking, so of coarse the player will be able to walk backwards into it. Change"event.getTo().getDirection()" to "player.getVelocity()" to fix this issue.
     
  7. Offline

    sniddunc

    Alright, I can't tell if the walking backwards problem is fixed because it is just acting as a sort of speed-bump, throwing the player up (if they jump) but it doesn't throw the player back. I haven't been able to figure this out without using getDirection(). Do you know of any ways to achieve this? I'm very sorry if I'm not getting this -- I'm new to vector manipulation.

    Code:
    player.setVelocity(player.getVelocity().multiply(-2));
     
  8. Offline

    mythbusterma

    @sniddunc

    That will just reverse the already reversed direction, like Zombie said.

    Pick a point on their side of the line (for example, their spawn) and create a vector from the player's location to that point, then normalise it and multiply it by like 5.

    That will throw them back towards spawn at 5m/s and can't be thwarted by facing the wrong way.
     
  9. Offline

    sniddunc

    How can I create a vector from one location to another?
     
Thread Status:
Not open for further replies.

Share This Page