How to code a gun (without using snowball in off hand )

Here is a quick coding guide on how to code a simple gun, it will take you about 5 minutes or so to code this (you need to have a basic understanding of the variables to code this, if you don’t know basic variable usage then read Atomseis’ really good guide explaining it, you can find it here)

  • Start off by placing a player right-click event then an If player holding item block [ Inside the chest put a item which will be used as the gun, for this guide I will be using a hoe ]

  • Now place a variable block and set it to “Get value from location” inside the chest put a variable named as %player% yaw inside the first slot, and use the game value “Current Location” in the location slot (marked by green-stained glasses) , then set the switch to the value “Yaw”, the chest will look something like this:
    image

  • Next, place another “Get value from Location” block and this time name the variable as %player% pitch and put it in the variable slot. In the location slot, put game value: ‘Current Location’ and in the Value switch, set it to “Pitch”

  • After that place a variable block set to “Set Location Values” inside the chest, in the variable slot put a variable named %player% launch and inside the Location Slot, put game value: Current EYE Location , Under the location, there are few slots like this :
    image

In the centre slot, there is a map named Set Mode Y click on it once to change the value from replace to relative (you will see an end rod) below that, there is an empty slot, inside that put a number value which should be 1 So that it will look like this:
image

  • After this place another variable block but set it to “Increment” , inside the variable slot put a variable named as GlobalName (saved) and in the number slot put value as 1
    image

  • Now use Game action spawn entity and put the entity as a snowball, and for the location slot, place the %player% launch variable from the set location value used earlier, after this press on the arrow on the side to go to the next page and inside the top left, on the entity name slot (present above the glass), put the name as the GlobalName. The final result will look something like this:
    image
    image

  • Now place a select object block
    image and set it to Select entities by condition by If Entity Name Equals after that take the GlobalName variable and put it inside this chest.
    image

  • Then place another variable block and set it to Set Location Values , inside the chest, in the variable slot put a new variable named %player% gun tp , and in the location slot put Game value: Current Location, now just to the right of the location slot, there is the Yaw Setting Mode , in that slot put the %player% yaw variable inside and to the right of it, there is Pitch inside that put the %player% pitch variable, and leave the rest of the slots empty [ Note: the pitch and yaw settings should be left at Replace
    image
    image

The final result should look like this
image

  • Now place a player action block and set it to Teleport, inside the chest put the %player% gun tp variable
    [ Then right-click the sign and set it to Selection]

  • Next place a player action block again and set it to Launch Forward and inside the chest put the value as 2 [ Then right-click the sign and set it to Selection]

  • If you want to set custom damage when the player gets hit by snowball then do the following →
    Place a player event and set it to Player damage by projectile then place an If Entity block and set it to Is Type and inside the chest put a snowball, Then place a player action and set it to damage player , inside the chest put the amount of damage you want to deal the player with, note: if you want to deal 2 hearts then put 4 inside, basically double the number by the hearts you want to deal.
    image

And basically its done and I didn’t think it would be this lengthy- :sweat_smile:

PS If there is anything wrong/missed out in this guide im sorry for that as this was made in a bit of a hurry.

5 Likes

Actually, there an even more accurate Guide

But unfortunately it is in Russian language.

You might need a web translator
Here: https://ruforum.mineland.net/t/vektornaya-sistema/23034

And also: https://ruforum.mineland.net/t/est-otvet-kak-sdelat-oruzhee-na-strelah/25375/2

1 Like

This helped me a lot tho.

1 Like

To increase the accuracy of the gun simply spawn the snowball on the top of the player first and then launch the player backward by 0.4+ blocks. After that select the snowball and launch it down by 1 block. This will make the snowball spawn at the “old” player’s current eye location so the snowball’s pathway wont be deviated. Explanation about the deviation simply because if the entity’s pitch is equal to the player’s pitch but the altitude is different then the current target vector of the entity will also be different from the player’s. If you want to make a laser gun then you can either make a manual vector system or through this method by making an incursive function and consecutively check the location of the snowball and select the objects that are near it.

4 Likes

dis one still damage player when player shoot on leg :v

1 Like

Because the player basically just shot themselves, what do you expect? It spawns on the player’s forehead and the destination is underneath the player’s leg.

1 Like

Also, who would even look down at what is underneath them? It is just a block.

1 Like

Function (a) → Set the value from the location (pos1 = Player’s location +1 on the y axis) → Set a variable (pos2 = Location of the whole block) → Get the distance between two points (deltaPos, pos1, pos2) → Get the value from the location (x1, pos1, x) → Get the value from the location (y1, pos1, y) → Get the value from the location (z1, pos1, z) → Get the value from the location (x2, pos2, x) → Find out the value from the location (y2, pos2, y) → Find out the value from the location (z2, pos2, z) → Set the variable (deltaX = x2 - x1) → Set Variable (deltaY = y2 - y1)
→ Set Variable (deltaZ = z2 - z1) → Set a variable (stepX = deltaX / deltaPos) → Set a variable (stepY = deltaY / deltaPos) → Set a variable (stepZ = deltaZ / deltaPos) → Set the value from the location (pos3 = pos1 + stepX by x, + stepY by y, stepZ by z)

And then we get the coordinates of a point at a distance of 1 block from the player along the line of sight.

If you want to increase this distance, then simply multiply the variables containing “step" by some number.

You can also make a function that will pass through each block located on this line and, for example, make a laser weapon that will shoot lines of particles.

2 Likes

Yeah use this if you’re making stuff that requires an algorithm to define the vector direction of player.

1 Like

This Was Very helpfull, althought, you cant shoot while flying with an elytra

1 Like

i dont understand a single thing

thats neat part u dont

cuz ur not old enugh he just used the basic vector formula delx = sqrt(x2-x1)

sorry im only 14 i dont know what vector even is last chapter i was on is pytagoras theorem

meanwhile me doing weird equations at 14

No spoilers (identdicaiotnodmtkeknfoiskjfek)