Solved Shoot a beam

Discussion in 'Plugin Development' started by KingFaris11, May 22, 2015.

Thread Status:
Not open for further replies.
  1. Hi, I'm trying to make a beam (of particles using the ParticleEffect Lib) shoot out from a location towards a player. However, I can't figure out how to make it so once the beam collides with the player, exit out of the loop. This 'distanceSquared' thing in the following current piece of code I've made doesn't work (I was testing out to see if it detects a 1 block radius).
    Code:
        public static void shootBeam(Location fromLocation, Player player) {
            try {
                if (player != null && player.isOnline()) {
                    if (fromLocation != null) {
                        Location toLocation = player.getEyeLocation().clone().subtract(0D, 0.1D, 0D);
                        Vector from = fromLocation.toVector(), to = toLocation.toVector();
                        if (from.getX() != to.getX() || from.getY() != to.getY() || from.getZ() != to.getZ()) {
                            Vector direction = to.subtract(from).normalize();
                            while (from.distanceSquared(to) > 1F) {
                                ParticleEffect.REDSTONE.display(ParticleEffect.ParticleColor.WHITE, from.toLocation(fromLocation.getWorld()), player);
                                from.add(direction);
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    I solved this by checking the distance between the original location and the current location of the particle. If it is beyond the range, exit out of the while loop.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page