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:
-
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 :
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:
-
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
-
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:
-
Now place a select object block
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.
-
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
The final result should look like this
-
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.
And basically its done and I didn’t think it would be this lengthy-
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.