Solved How can I invert the velocity of an arrow?

Discussion in 'Plugin Development' started by MegaCrafter10, Nov 24, 2019.

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

    MegaCrafter10

    Hi, I have this piece of code which shoots an arrow at a player (or at least it is supposed to) from an armor stand but for some reason, it always shoots in the opposite direction.

    Code:
    Entity arrow = turret.getWorld().spawnEntity(turret.getLocation().add(0,2.5,0), EntityType.ARROW);
                    Vector motion = target.getLocation().toVector().subtract(turret.getLocation().toVector());
                    arrow.setVelocity(motion); 
    (turret represents the armor stand and target represents the closest entity)

    I tried to multiply the motion by -1 to invert it (arrow.setVelocity(motion.multiply(-1))) but it still shot in the opposite direction. Why does this happen?
     
  2. Offline

    RuthlessJailer

    I ran that exact code (except shot it at a pig) and the only problem I encountered was it shooting too high. You said "shoot in the opposite direction" does that mean it shoots away from the player?
     
  3. @MegaCrafter10 I'm not sure about the .subract part. I would try to create a vector this way: (turretX - targetX, turretY - targetY, turretZ - targetZ). Try this and compare the results. As I said, .subtract could make something similar but this makes more sense to me

    @RuthlessJailer That's probably because the vector is calculated 2.5 blocks below the actual arrow, should fix that too
     
  4. Offline

    MegaCrafter10

    @RuthlessJailer correct, it was shooting away from the player and I think I know why. I was in creative mode so the arrow must have reflected back the other way.

    @DerDonut thanks for the info, I got it to work using what you said.

    Here is the version I am using and it works perfectly:
    Code:
    Vector motion = new Vector(turretLoc.getBlockX() - targetLoc.getBlockX(), turretLoc.getBlockY() - targetLoc.getBlockY(), turretLoc.getBlockZ() - targetLoc.getBlockZ());
                    Entity arrow = turret.launchProjectile(Arrow.class);
                    arrow.setVelocity(motion.multiply(-1));
    the arrow was colliding with the armor stand if I didn't give it an offset and if I did, the arrow would fly to hight above the player so I used turret.launchProjectile() which works much better.

    Thanks for the help.
     
Thread Status:
Not open for further replies.

Share This Page