Getting the block in front of a player (Or location) using vectors.

Discussion in 'Resources' started by ChipDev, Oct 29, 2014.

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

    ChipDev

    Hello people. I'm going to try to take another shot at this section...

    Some people are now making FPS games, (Like me) and a key part is shooting. Lets say you want to shoot a snowball, you spawn it inside of the player, then directly stops because it hits the entity. Then you have to do a whole different event testing the entity shooter compared to the victim etc. But, now you can spawn the entity IN FRONT of the player, using vectors to spawn it.
    Locations are changeable in this tutorial, for example, eye location.

    Lets get started :D

    First get the location of the player.. I prefer the eye location.)
    Code:java
    1. //Get regular player location.
    2. Location eyelocation = player.getEyeLocation();
    3.  


    Then, we get the player's direction and turn it into a vector (It already is))
    Code:java
    1. Vector vec = player.getLocation().getDirection();


    then, add that to the eyelocation!)
    Code:java
    1. Location frontlocation = eyelocation.add(vec);


    now you can spawn whatever and use that frontlocation! (And set its direction to frontlocations' direction))
    Code:java
    1. Entity bullet = player.getWorld().spawnEntity(frontLocation, EntityType.SNOWBALL);
    2. //Spawns a snowball at front location
    3. bullet.setVelocity(frontlocation.getDirection().multiply(4.0));
    4. //Sets velocity of bullet to the direction that the player is facing.
    5.  

    Thanks for reading, and yes, you are done!
    Thank you for using Constructive criticism, and I'll cya later, bye!
     
  2. Offline

    Deleted user

    ChipDev
    The eye height changes depending on whether the player is sneaking.
     
  3. Offline

    ChipDev

    True. maybe I should change to getEyeLocation().
     
  4. Offline

    Skyost

    This is the best way.
     
  5. Offline

    d3v1n302418

    ChipDev
    Code:java
    1. player.launchProjectile(Snowball.class).setVelocity(player.getEyeLocation().getDirection().multiply(4.0D);

    How is your code any different from this?

    It's done in two lines

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 8, 2019
  6. Offline

    ChipDev

    That was my example,
    You can spawn blocks at frontLocation, My tutorial is getting the location in front of the player.
     
  7. Offline

    d3v1n302418

  8. Offline

    Aqua

    Code:
        public static Block getBlockAtDistance(LivingEntity entity, int range, boolean stop_at_solid) {
            BlockIterator iterator = new BlockIterator(entity, range);
         
            while (iterator.hasNext()) {
                Block b = iterator.next();
             
                if (iterator.hasNext()) {
                    if (stop_at_solid) {
                        if (b.getType() == Material.AIR
                                || b.getType() == Material.VINE
                                || b.getType() == Material.TORCH
                                || b.getType() == Material.DEAD_BUSH
                                || b.getType() == Material.SIGN_POST
                                || b.getType() == Material.WALL_SIGN) {
                            continue;
                        } else {
                            return b;
                        }
                    } else {
                        continue;
                    }
                } else {
                    return b;
                }
            }
         
            return null;
        }
    I made this, does basicly the same, just somewhat more usefull...

    Or this one:
    Code:
        public static ArrayList<Location> getLine(Location start, double range, int locations_between) {
            Vector dir = start.getDirection();
            double step = range/locations_between;
            ArrayList<Location> temp = new ArrayList<Location>();
           
            for (int i=0; i < locations_between; i++) {
                temp.add(dir.add(dir.clone().normalize().multiply(step)).toLocation(start.getWorld()));
            }
           
            return temp;
        }
     
  9. Offline

    Monkey_Swag

    every time ChipDev makes an attempt at a tutorial he gets laughed at.... poor guy xD
     
    Skyost likes this.
  10. Offline

    ChipDev

    Meh, Im used to it :3
     
  11. Offline

    hubeb

Thread Status:
Not open for further replies.

Share This Page