How to launch projectile in direction your looking.

Discussion in 'Plugin Development' started by JavaNoob, Sep 30, 2020.

  1. Offline

    JavaNoob

    Hi! I had a plan to make a few custom crafting recipes and I wanted to make something cool for a custom blaze rod to shoot a ghast fireball every time you right click. I already know how to register right clicks and stuff I just need to know how to launch the projectile in the direction your cursor is facing. (Not in a certain direction like north east south west, but rather wherever the cursor is pointing, even straight up.)
     
  2. Offline

    KarimAKL

    @JavaNoob I think you can spawn it with the vector gotten from Player#getDirection().
     
  3. Offline

    JavaNoob

    Does that apply to just the direction the entire player is facing, or the cursor angle?
     
  4. Offline

    Kars

    Isnt that the same thing?
     
  5. Offline

    JavaNoob

    I meant like if the player is facing north east south or west, no like if the cursor is like at an angle or up or down, if you know what i mean.
     
  6. Offline

    KarimAKL

    @JavaNoob It is not a face (i.e. north, south, etc.), it is what you want.
     
  7. Offline

    JavaNoob

    Thank you! I looked in the javadocs thought, I I couldn't find an extension for the player class that was getDirection. Am I just dumb, or is it called something else, or does it not extend the player class?
     
  8. Offline

    KarimAKL

    @JavaNoob I believe it's actually Player#getLocation()#getDirection(). My bad.
     
  9. Offline

    JavaNoob

    That is perfectly ok. Thank you both so much for all your help.
    Edit: It wont work, unfortunately. I can't figure out how to cast it to the projectile.
    Can you see anything?
    Code:
     
        @SuppressWarnings("deprecation")
    
        @EventHandler
    
        publicvoidonInt(PlayerInteractEventevent){
    
            Playerplayer=(Player)event.getPlayer();
    
            varloc=player.getLocation().getDirection();
    
            finalCraftFireballfireball=(CraftFireball)player.getWorld().spawn(player.getLocation().toVector().toLocation((World)player.getLocation().getDirection()),Fireball.class);
    
            if(event.getAction()==Action.LEFT_CLICK_AIR){
    
                if(player.getItemInHand().equals(newItemStack(Material.BLAZE_ROD))){
    
                    player.launchProjectile(fireball);
    
                   
    
                }
    
                return;
    
            }
    
       
     
    Last edited: Sep 30, 2020
  10. Offline

    KarimAKL

  11. Offline

    JavaNoob

    1. Sorry, when I posted it i didn't realize XD.
    2. And yes, now I have. So the location part is just where to spawn the fireball, but it doesn't affect the direction of where it fires. I will keep messing with it, and if I can't find the solution, I will come back here XD.
     
  12. Offline

    KarimAKL

    There's no parameter of the type Location, there's only Class and Vector.

    The spawn location for the projectile is the player's location, since you spawn the projectile using Player#launchProjectile(...).
    The Class parameter is the projectile type.
    The Vector parameter is the velocity (i.e. the direction & the speed).
     
  13. Offline

    JavaNoob

    It works! Thank you! But what if I wanted to launch the player in the direction they were looking? I tried the player.setVelocity(new Vector(player.getLocation().getDirection())); but it didn't work.
     
    Last edited: Oct 1, 2020
  14. Offline

    KarimAKL

    @JavaNoob
    1. There's no Vector(Vector) constructor.
    2. Location#getDirection() returns a Vector, there's no need to create one.
     
  15. Offline

    JavaNoob

    Oh lol sorry im so dumb.
     
  16. Offline

    KarimAKL

    @JavaNoob Don't worry about it. :p Did you get it working?
     
  17. Offline

    JavaNoob

    Yeah, I did! It works great! Thanks again!
    I got it so that it launches the fireball, then I just multiplied the vector by -1 to make the recoil for the player. It works perfectly!
     
    KarimAKL likes this.

Share This Page