Solved teach me how to use rayTraceBlocks()

Discussion in 'Spigot Plugin Development' started by Domini407, Feb 28, 2020.

  1. hello I'm trying to use rayTraceblocks() after several attempts failed, I decided to ask for some examples. :cry::D

    I'm trying to hit a block and get the block location from a start location , example from point A make a straight ray trace line to point B and gets location of point B, I wan't to work with straight lines , up, down, left or right.


    Vector blockLocation= world.rayTraceBlocks(start,direction , maxDistance).getHitPosition();

    Parameters:
    start - the start location = Location
    direction - the ray direction = Vector
    maxDistance - the maximum distance = Double
    Returns:
    the ray trace hit result, or null if there is no hit

    getHitPosition();
    Gets the exact position of the hit.

    sorry for those colors
     
  2. SteelPhoenix

    Moderator

    So what is your question? You have a starting point, a direction you want to look at and the maximum trace distance as params.
     
  3. SteelPhoenix

    Moderator

    You just use a Vector with the direction you want to trace
     
  4. thanks for the help , I will wait for somebody to provide an example on how to do that, I know I have to make a vector with a direction....but I don't know how to to that
     
  5. Yeah honestly, what's up with that?
     
  6. lol no comment
     
  7. drives_a_ford

    Moderator

    This section is not for somebody to soonfeed you code. If you've got an issue, we'll point you to the right direction, but you'll need to do the legwork yourself. The readme of this section also clearly states:
    And if you do indeed have an issue you cannot resolve then keep in mind that some of the first questions someone will ask are:
    What have you tried?
    What's not working exactly? Do you get an error in your IDE or in console?
    What kind of result are you looking for?
    What kind of results does your current attempt produce?
     
    • Agree Agree x 1
  8. thank you for inform me I simply needed help on how to use rayTraceBlock maybe an example.
    thread Closed
     
    #10 Domini407, Feb 28, 2020
    Last edited: Feb 28, 2020
  9. SOLVED!

    how to get the position of a block using a start location and direction with rayTraceblocks() and gethitPosition(), pretty simple and hope this will help anyone looking for how to make ray trace.

    Code (Java):
    //whatever your start location is
    Location startLocation = new Location(world,-106,45 ,63 );

    //we have a vector x y z
    // (-1, 0, 0) it means ray will trace towards negative X.
    // (0, 1, 0) it means ray will trace towards Positive Y.
    // (0, 0, 1) it means ray will trace towards Positive Z.
    //all in a straight line.

    //negative X.
    Vector direction = new Vector(-1, 0, 0);

    //max distance of ray
    double maximumDistance = 10.0;

    Vector hitPosition = world.rayTraceBlocks(startLocation ,direction , maximumDistance ).getHitPosition();

    //and finally getHitPosition(); gets the exact position of the hit and return a vector