Started by
Planqqc
on
Mon, 01/02/2023 - 02:45

**Topic category:** Help with modding (Java Edition)

I'm trying to make a laser attack weapon, the damage system works just fine but I want to spawn particles along the line cast from my sight to my target. Does anyone know of a way to do this?

you could make a variable and shift it forwards in a loop and check if the block at the location is not air

When you think about moving in 3D, you use something called a vector.

3D vectors are usually written with x, y, z coordinates.

To move forward in a direction, you want what's called a "normalized" or "unit" vector, which takes the direction you are facing and shortens it down to a single "unit". Usually these are exactly 1 meter/block/etc long, so if you add 2 of them together you get a distance of 2 meters/blocks/whatever in that direction.

To make particles in a straight line, you would use the unit vector of the direction the player is facing, and just keep adding 1 unit to it and making more particles until you hit a solid block, like what redwoodsteve mentioned above.

To get a unit vector:

Step 1 - get the position you are looking at and subtract the player's position from it. This is your delta vector.

Step 2 - we need to get the magnitude of the delta. You can do that with this formula: mag = sqrt( (x * x) + (y * y) + (z * z))

Step 3 - divide the x, y, and z values of your delta vector by the magnitude - this new vector is your unit vector

Here's a super simple example:

If the player is looking straight forward on the x axis, maybe they are looking at a block at X12, Y0, Z0

We'll pretend they are standing at X2, Y0, Z0

We subtract 2, 0, 0 from 12, 0, 0 and get a result of 10, 0, 0. This is our delta

Next we do some math:

mag = sqrt( ( 10 * 10 ) + ( 0 * 0 ) + ( 0 * 0 ) )

which means mag = sqrt( 100 )

which means mag = 10. Wow, it's 10 blocks away - that's neat

So now we divide 10, 0, 0 by 10 and get 1, 0, 0 as our unit vector

This means if we keep adding this value, we'll keep moving 1 block forward

Since "forward" in our example is a straight line on the x axis, hopefully it makes sense that adding 1 block on x moves you forward 1 block on x lol

This math also works for angles that are a bit harder to visualize or quickmaths, though

This is working really well, however the particles are only generated when the player is aiming at an entity with in the positive X and Positive Y quadrants, when aiming at an entity in the double negative quadrants the beam will fire at the correct Y value but be "moving" behind the player. I tried to make a fix to this as I had the assumption that it was due to losing the negative value within step 2 "mag = sqrt( (x * x) + (y * y) + (z * z))". After making a statement to check if the value was previously negative, then reconfirming it to a negative value then implementing that it still doesn't work. It simply does not generate particles in any of the other 3 quadrants now. I hope I explained my problem well enough. If not I can upload a screenshot of the procedure I made. Many thanks to the help so far!