Y axis problems...

Discussion in 'Plugin Development' started by DatCookiez, Jan 17, 2015.

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

    DatCookiez

    Hello guys,
    Just trying to create a fireball that fires from above a player (15) blocks then charges down toward them. Obviously not having much luck and I'm new to Vectors and Direction.. Current code:

    Code:
            double x = 0;
            double y = 15;
            double z = 0;
                     
            double vx = x;
            double vy = y-15;
            double vz = z;
           
            Vector dir = new Vector(vx, vy, vz);
           
            ItemStack sword1 = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta sword1Meta = sword1.getItemMeta();
            sword1Meta.setDisplayName("Pyro's Sword");
            sword1.setItemMeta(sword1Meta);
            for (Entity v : p.getNearbyEntities(20, 20, 20)){
            if (v instanceof Player){
               
                Location loc = v.getLocation();
               
            if (Main.pyro.contains(p)){
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (p.getItemInHand().getType() == sword1.getType()){
                    /*if (b.getType().equals(allItems.clone())){*/
                        LargeFireball fb = (LargeFireball) loc.getWorld().spawn(loc, LargeFireball.class);
                        fb.teleport(new Location(v.getWorld(), x, vy, z));
                        fb.setIsIncendiary(true);
                        fb.setFireTicks(20*4);
                        fb.setVelocity(dir);
                                }
                            }
                        }
                    }
                }
    Thanks in advance! <3
     
  2. Offline

    WinX64

    With your current code, the fireball will always be teleported to a fixed place (0, 0, 0), regardless of your player's location.
    Try using the player's location as reference instead, for example:

    Code:
    double y = p.getLocation().getY() + 15;
     
  3. Offline

    Rocoty

    You have to understand how vectors work and how they are handled in terms of velocity. Your direction vector is created from the values of vx, vy and vz. This may look all perfectly fine, except it isn't. You want the direction to be downwards, however, your vector is [vx, vy, vz] where vx = x = 0, vz = z = 0 and vy = y-15 = 0, i.e [0, 0, 0], so you have basically made yourself a null vector. A vector of no direction and no magnitude.

    You want a vector with a slight downwards direction. So, we know the Y axis accounts for up and down, and we also know that a smaller Y value means further down. In terms of velocity, a vector of [0, 0, 0] means a completely still velocity. A vector of [0, 1, 0] implies a slight upward velocity, therefore a vector of [0, -1, 0] implies a slight downward velocity. Try to understand what all this means and play with the values.
     
  4. Offline

    DatCookiez

    @WinX64
    It just shoots the Fireball upwards

    @Rocoty
    Doing this still has the same problem
    Code:
     double x = 0;
            double y = p.getLocation().getY() +15;
            double z = 0;
                     
            double vx = x;
            double vy = y-5;
            double vz = z;
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  5. Offline

    WinX64

    You still have your x and z as fixed values. This way the fireball will simply spawn 15 blocks above your player's Y, but still at point X: 0 and Z: 0
     
  6. Offline

    DatCookiez

    @WinX64
    So adding 15 onto X & Z still does nothing.
     
  7. Offline

    WinX64

    If you want the fireball to be exactly above the player(with some Y offset), you don't need to add anything to X or Z, just use the current player's position.
     
  8. Offline

    DatCookiez

    @WinX64
    By doing this, the fireball is still just spawning at the players feet.
    Code:
    double x = p.getLocation().getX();
            double y = p.getLocation().getY() +15;
            double z = p.getLocation().getZ();
     
  9. Offline

    WinX64

    If i recall correctly, in your first code, you were using x, dy and z to create a new location, while dy (y - 15). That may be causing the problem.

    Anyway, an easier way to work with offsets in this scenario is using Location#add. This for example, would be what you're looking for:
    Code:
    Location l = p.getLocation().add(0, 15, 0);
     
  10. Offline

    DatCookiez

    @WinX64
    How would I then slam it down?
    It works at the moment above a players head <3
     
  11. Offline

    WinX64

    Rocoty wrote a nice explanation about it just above. Everything you will need is there.
     
    Rocoty likes this.
  12. Offline

    Rocoty

    @DatCookiez I urge you to try a bit on your own. Get to know how vectors work. Play around a bit. It really will teach you something if you're curious.
     
Thread Status:
Not open for further replies.

Share This Page