Guns have been a staple of minecraft command creations for years and they range in complexity from simple potion effects upon right-clicking to complex damage systems custom sounds and randomized recoil but no matter how experienced you are with data packs you will almost certainly be implementing a system of Raycasting in order to accomplish this stick around and i’ll show you how to implement a full raycast system from start to finish In this context raycasting is essentially drawing a single line in the world which starts at a point of your choosing and this has infinite usage beyond the obvious weapon creations such as to detect the block the player is looking at today i’ll be showing you a full raycast System which you could build off of to develop your own packs make sure to stick around until the end so you could fully understand how exactly the pack works and how you could adapt it for your own creations as usual the download for the pack that I made will be down in the description below i’ll start with the high level overview this system i will be showing you includes three main functions for a total of just nine commands the first one starts our raycast and sets up each individual raycaster before each ray is casted this calls the Next function which is the main raycast command this second function repeatedly calls itself at a more forward position checking to see hit a target or a block and stopping if it reaches its maximum range if an entity is detected it runs the third function which acts upon the target that the Raycast collided with in my example pack i also include a form of headshot detection which i showed last video let’s get into the actual commands i’m not going to go in the order that the commands run but rather in the order that’s easiest to follow logically if you were building this from scratch Hopefully that makes it easier for you guys to understand exactly how this works and why certain commands are needed we’ll start with the function that will start the raycast when called upon i’ll call this start raycast by default running the raycast function from an entity will run the raycast of The entity’s actual coordinates which is usually at the feet you probably want to run the raycast from the head so we’ll anchor to the eyes of the entity starting the raycast in the raycast function before we actually create any of the raycast’s movement we first check for entities by using the Dx concept i explained in the last video make sure to go check that out i explained why this is more accurate than distance and how exactly it works for this example the offset will be negative 0.99 to give me a tiny projectile the size of 100th of a block If you want a larger projectile for an rpg or 50bmg round then feel free to change the offset for a massive raycast projectile larger than a full block use just a single dx volume detection of a size over 1 and make sure to offset the detection by Half the size to make sure it’s centered on the raycast when our raycast collides with an entity we then run our collide function now there are currently two issues with this detection which we need to fix first of all after adding the actual raycast movement it will be targeting Entities that may not necessarily be intended targets of the raycast we’ll filter entity types by using an entity type tag which i’ve shown before in the data pack we create a tags folder and then an entity types folder i add a file called not mob and it Contains entities which are not going to be targeted by our raycast inside the actual raycast function we filter these out by excluding our entity type file the second issue is that the entity or player launching the raycast would be immediately detected and you’d essentially shoot yourself in the face Obviously we don’t want to be shooting ourselves in the face so in the start raycast function we need to grant immunity to the raycaster entity and we’ll do that by giving them a special tag called raycasting which we’ll remove after this individual raycast is finished inside the raycast function we want to Make sure that any targeted entities don’t have this tag now the collide function will only be run on entities that we want to be targeting we also probably want to limit the range of arraycast and to do that we need to be counting exactly how far it has gone At any point in time first we need to create the scoreboard which will hold the values in our load initialization function we’re simply going to create a scoreboard which i’ll call tfrc which will hold our distance data since our raycast will be moving forwards a bit every time the raycast function runs We could put a counter in the raycast function i do scoreboard players add distance tf rc1 dot distance is simply the name of a fake player which will be storing the number of times that the raycast function ran i also added particles here now we can actually cast the ray by making the Function call itself and move forwards we use an execute command starting with if score to check that before moving forward we haven’t yet exceeded the maximum range we then use vector coordinates to position the command a little bit forwards to check the next portion of the ray You can also choose to add rotation here if you want projectile drop-off the forward increment number you use determines accuracy since a smaller increment means more frequent checks which means less of a chance of accidentally skipping over a tiny corner of the hitbox this also determines the number that you Use to compare with your distance value the maximum distance that the raycast will travel is the increment value you choose times the maximum dot distance value that you allow the next part of this command is checking that the block it will move into is something that the raycast could Actually pass through you probably don’t want just air here so we could use a block tag similar to the entity type tag from earlier in the tags folder i make a blocks folder and paste in a file which lists a bunch of transparent blocks like flowers and saplings This detection will make sure that the raycast only moves forward if a penetrable block is in front of it feel free to add to this list if you have a powerful weapon that can break through solid blocks like maybe snow or wood if all of these conditions are met the Raycast function runs itself again our last function is our collide function this function which we ran from the raycast function could really be anything you want from dealing damage to blinding the target to turning it into a chicken riding a pig riding a bat the only thing you really need is to Make sure that at the end you set the dot distance value to your maximum in order to stop the raycast function from running again this ensures that your collide function will not run multiple times on the same entity or penetrate through if you do want it to penetrate then leave it out You will have to prevent it from running multiple times on the same entity though maybe by tagging them with immunity i also included a headshot detection system which uses dx and is somewhat similar to what i showed last video so make sure to check that out the last Thing we have to do is go back to the start raycast function if you’re unfamiliar with how the order of code generally executes here’s how it works the start function runs its normal commands but then it runs the raycast function since two functions don’t run at the same time The start function essentially waits for the raycast function to terminate however instead of terminating at the last command the raycast function calls itself and then it waits for itself to finish but then it calls itself again so this loop repeats until finally one of the calls to the Raycast function fails to call itself again for example if an entity or a solid block is hit when this happens all of the previous calls to the raycast function terminate and the start function finally gets to continue where it left off which was when it had to wait for the First raycast function to finish with it finished it means that anything we add in the start function at this point will execute after the entire raycast is already over once the raycast ends we want to remove the raycasting tag from the entity who just finished raycasting to make sure that only mobs Actively raycasting are made invulnerable also we want to reset the range score to prepare for the next raycast so that it starts at 0. that’s basically it for the basics of setting up a full raycast system again the download is in the description and inside of the actual files i left a Bunch of comments so that you’ll be able to remember what you could change to customize some of the different aspects of the pack for yourself just as a refresher on actually using the pack run the start raycast function on the end of t launching the array and put your damage functions or whatever Inside of the collide function that’s it for today make sure to leave a like and subscribe for more content i’ll see you guys in the next video you Video Information
This video, titled ‘EASY GUNS in Minecraft – Full Raycast System Tutorial || Minecraft Data Packs’, was uploaded by Timber Forge on 2021-07-17 21:12:24. It has garnered 39037 views and 1188 likes. The duration of the video is 00:08:38 or 518 seconds.
Everyone loves adding guns to Minecraft, and today I’ll show you a full raycasting system which you can use to make your own unique weapons, tools, or any other cool creations. You can customize this to your liking, and add additional commands to build off of the basic raycasting framework. Some ways to add onto this is by adding custom damage systems, vienminer, custom bullet impact sounds, dynamic range, and usage by custom mobs.
SPECIAL THANKS TO ApexHosting! They have amazing servers and great service. Use this affiliate link to get your own server and to support me as well: https://billing.apexminecrafthosting.com/aff.php?aff=4317
Intro – 0:00 General Overview – 1:02 Start Raycast – 1:57 Raycast Function – 2:14 Fix Raycast Targetting – 2:58 Limit Range – 3:59 Actually Casting the Ray – 4:39 Collide Function – 5:58 Cleaning Up in Start Function – 6:41 Outro – 7:57
Download this pack: https://drive.google.com/uc?export=download&id=1d8utTVsL7qbhfEXE5-HYwtF49j7qGPUz
The AK in the thumbnail came from Elidhan’s Guns datapack, check it out: https://www.planetminecraft.com/data-pack/multiplayer-amp-survival-friendly-guns-1-17/
Check out my website about data pack making! https://tinyurl.com/timberforge
Join our discord for help and inspiration! https://discord.gg/bwBsSbr
#datapacks #minecraftmods #minecraftdatapacks