Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to shoot a piece of tnt from the players face and then let it explode when it hits the target block.

        if (cmd.getName().equalsIgnoreCase("cannon")) {
        if(!perrmissionCheck(sender, "Needs.cannon", "cannon")){
            return true;
        }
        if (!(sender instanceof Player)){
            sender.sendMessage(nameC+"You must be a player to preform this command.");
            return true;
        }
        Block block = ((Player) sender).getTargetBlock(null, 50);
        Location location = ((Player) sender).getLocation();
        //float power = 4;
        //if (args.length == 1){
            //power = Float.parseFloat(args[0]);
        //}
        sender.sendMessage(name+"BOOM!");
        double x = block.getX();
        double y = block.getY();
        double z = block.getZ();
        TNTPrimed tnt = (TNTPrimed) ((Player)sender).getWorld().spawn(location, TNTPrimed.class);
        Vector v = new Vector(x, y, z);
        tnt.setVelocity(v);
        //block.getWorld().createExplosion(location, power);
        //block.getWorld().spawn(tntLoc, TNTPrimed.class);
        return true;
    }

This kinda just ramdomly throws the tnt and then exploads i want to get where the player is looking and then shoot it that way if someone could also describe how the args work for new Vector(arg0, arg1, arg2) i would appreciate it...

Thanks any help is great

EDIT I relize this is really old but i wanted to share how I did it,

    int v = 2;

    if (args.length > 1){
        v = Integer.parseInt(args[0]);
    }

    final Vector direction = user.getEyeLocation().getDirection().multiply(v);

    TNTPrimed tnt = user.getWorld().spawn(user.getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), TNTPrimed.class);

    tnt.setVelocity(direction);

    user.sendMessage(name + "Boom!");
    plugin.inGameMsg.msgAdmins(plugin.chatDark_AQUA + user.getName() + plugin.chatGray + " used cannon command!");
    return true;

this sends the tnt right where the player is looking

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The three arguments taken by Vector are its (x, y, z) components. Here's a visual representation of a Vector in 3D space:

enter image description here

A vector can be used to store either a displacement (e.g. movement) or a position. In this case since you're passing the vector to setVelocity() it's a displacement - it specifies in which direction the TNT will be moving.

But it's probably not working because your Vector is currently holding the position of the target block in the world, and what you want is the direction the player is facing.

Try to see in the Player class if there's already a way to get a Vector representing the way it's facing. Otherwise you'd need to calculate one from the player's rotation values.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.