Solved Getting direction of projectile

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Feb 23, 2020.

  1. Code (Text):

     @EventHandler
      public static void onCollison(ProjectileHitEvent e) {
     
        //if block is hit do nothing
        Entity entity = e.getHitEntity();
        if(entity == null) {
          return;
        }

        if(e.getEntity().getType() == EntityType.SNOWBALL){

          double power = -1.5;
          entity.setFireTicks(100);
          Vector currentDirection = entity.getLocation().getDirection().normalize();
          currentDirection = currentDirection.multiply(new Vector(power, power, power));
          entity.setVelocity(currentDirection);

        }
     
    Hi, I am trying to make an entity get pushed towards a certain direction when hit by a projectile. I want the direction to be dependent on the direction the projectile came from. For example if i throw a snowball at Steve I want steve to fly towards the direction the snowball came from. Or if Steve gets hit by an arrow I want steve to get pushed in the opposite direction the arrow came from, same as how arrows already worked.

    Right now I have this ProjectileHitEvent to push the player in the current direction they are facing. I would like to change it to being dependent on the direction that the projectile is flying. So if a projectile is flying west and hits someone, then I want the direction to be west. That way I can say velocity (+ number) to push them in the same direction and velocity (- number) to pull them in. I am thinking I might have to make an event like EntityShootBowEvent
    for when the arrow is shot but I'm not sure exactly where to extract the direction that the arrow is flying.

    I am very new to Vectors in coding so go easy on me.
     
    #1 Poofyjerry, Feb 23, 2020
    Last edited: Feb 23, 2020
  2. Hi, you can just get the direction of the projectile instead of the hit entity, for that you want to change this:
    Code (Java):

    //Vector currentDirection = entity.getLocation().getDirection().normalize();
    Vector currentDirection = e.getEntity().getVelocity().normalize();
     
     
  3. Hey this worked perfect thank you man!
     
  4. Can you mark the thread as "Solved" :)

    Happy Coding!