Custom item, same velocity as Arrow.

Discussion in 'Plugin Development' started by Jazed, Mar 17, 2013.

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

    Jazed

    Hi,

    I want to transform a arrow into a custom item, this works but the velocity and speed of shooting the item doesn't act the same as an arrow.

    This is my code:

    I want it to have the same velocity as an arrow and the same cooldown effect.

    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onProjectileShoot(ProjectileLaunchEvent e) {
       if (e.getEntity().getShooter() instanceof Player) {
       Player p = (Player) e.getEntity().getShooter();
       if (e.getEntity() instanceof Arrow) {
       Arrow a = (Arrow) e.getEntity();
       if (GameManager.getInstance().classes.get(p).equalsIgnoreCase("wither")) {
       e.setCancelled(true);
       p.launchProjectile(WitherSkull.class).setVelocity(a.getVelocity());
       }
       }
       }
        }
    
     
  2. So no errors and the code actually launches a wither skull as projectile ?

    Well, you should make sure that a.getVelocity() actually returns a non-0 velocity because the event triggers before the arrow is launched.
    Or you can test using a hardcoded velocity like new Vector(0, 5, 0), that should go up kinda fast (or increase the number and re-test).

    If velocity is valid and/or hardcoding velocity doesn't work try using a delayed task to apply the velocity.
     
  3. Offline

    Jazed

    Digi, no errors and the event launches it perfectly, I'd rather not hard code the velocity as I want the velocity to be according to the velocity of the arrow, making the velocity less if the bow not charged alot and making the velocity higher as the bow was charged alot.
     
  4. Hardcoding the velocity is only for testing to figure out if you can actually apply velocity to that entity at that point.
     
  5. Offline

    Jazed

    Setting the velocity works, the wither skull shoots up into the sky, quite fast.

    Code:

    Code:
    p.launchProjectile(WitherSkull.class).setVelocity(new Vector(0, 5, 0));
    
    This is the arrow velocity:

    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Maybe the arrow has something special that makes it fly, a simulated aerodynamic behaviour or something..

    Try with that vector multiplied by 2 or more, then it should reach a similar value to that hardcoded vector's speed.

    I guess it's more about trial and error now, unless someone knows exacly what's up with arrows flying faster.
     
  7. Offline

    Murderscene

    Could you try using setPassenger() to set the wither skull as a passenger of the arrow?

    Not sure if it's what you are after as you will have both an arrow and a wither skull now. But it may work :)

    Code:
    a.setPassenger(witherskull)
     
  8. Offline

    Burnett1

    Can you not just get the arrows velocity.
    Vector v = a.getVelocity();
    then set the wither skull to that?
     
  9. Offline

    Jazed

    But didn't work out, the wither skull just shout out and went into a straight line, and did not act like a arrow.

    Code:
    p.launchProjectile(WitherSkull.class).setVelocity(a.getVelocity());
     
  10. Offline

    Murderscene

    Just a thought. Have you tried removing the arrow before you launch the wither skull? Maybe it's colliding with the arrow and it's messing it up.
     
  11. Jazed
    Hmm, I may've misunderstood the issue... does it go in a straight line without falling to the ground (no gravity) ?

    If that's the case then ignore what I previously said because I thought it didn't have enough velocity to go far enough like an arrow and it fell too early.

    So, if it doens't have gravity the most realistic way you can do it is either enable gravity on it somehow or attach it to the arrow like Murderscene suggested... you could also apply downards velocity to it but it will just go in a straight line towards the ground, not in an round arc like gravity affects it.

    EDIT:
    Or yeah, like Burnett1 said, a task (not a loop :p) that adds small amounts of velocity downwards, should be easy to do.
     
  12. Offline

    Burnett1

    Well then you just need to make a loop that decreases the arrows y velocity until it hits the ground.
     
  13. Offline

    Jazed

    Decided to go with a delay on the bow.
     
Thread Status:
Not open for further replies.

Share This Page