• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • Minecraft 1.16.220 Update + Bug/Crash Fixes
    Added MCPE 1.16.220 version and fixed crashes for multiple plugins. You can view the full changelist here.
  • Forum Updates - 4/11/2021
    Various changes have been made to the forums and a few categories. You can view the full changelist here.

Using the new math functions

Warning!
Hello , there have been no replies in this thread for more than 30 days.
Please make sure you have a valid reason before you reply to this thread or you may face moderation action.
#1
In this post I'll explain how to get the nearest block or player a player is looking at. Keep in mind the command below hasn't been tested yet nor does it do much. In order to use it, you need to edit things. You might be wondering "Why would I ever want to know what someone sees?"... It's actually really useful. You can for example create a water gun with it by using particles (included in the code) or you can create a painting worldedit tool. Do you know any other ways to use this? Let me know in this thread!


Here I'm just creating the command and defining the variable used in the loop.
Code:
cmd create looking Example command to get the nearest block or player a person is looking at
cmd add looking %num% = 1
I jump to line 100 to keep things clear. If I wanted to insert a line before it I don't have to add cmd insert at the end of my document. Though, since the new update, inserting is no longer a problem I still like to do it this way.
Code:
cmd edit looking 99 #Main loop
Here I use the formula's for calculating spherical coordinates to cartesian coordinates. *English please*
Here I use a formule from internet that uses the looking direction and the distance from your head to see where you're looking at. Since I don't know the distance, I'll just check for every block on your viewing line in a range of 100 blocks.
If you want to know what someone else is looking at, you can save their yaw and pitch before the loop starts and use it here instead of the predefined variables %yaw% and %pitch%.
Code:
cmd add looking %xx% = int(%num% * sin(%pitch%) * cos(%yaw%))
cmd add looking %yy% = int(%num% * sin(%pitch%) * sin(%pitch%))
cmd add looking %zz% = int(%num% * cos(%pitch%))
As I mentioned in the intro, I was going to add particles, so here they are.
Code:
cmd add looking #Particles (optional)
cmd add looking ascon /particle dripwater %xx% %yy% %zz% 1 1 1 10
Here I'm checking whether they look at a block. If there is no block, the block ID would be 0 (which is the id of air). In that case the loop continues. If it's not 0, it ends the loop and goes to the end. The %found% variable contains whether the command found a block or a player.
Code:
cmd add looking #Find block
cmd add looking %block% = blockinfo(%level%, %xx%, %yy%, %zz%)
cmd add looking %id = %block%[\"id\"]
cmd add looking %found% = \"block\"
cmd add looking if %id% != 0 then goto 500
Here I quickly create a loop through the online players to see if their coordinates matches the coordinates I found. If I found something I go to the end of the command and stop the loop, just like with the block part.
Code:
cmd edit looking 199 #Look for player
cmd add looking %num2% = 0
cmd add looking %end% = size(onlineplayers())
cmd add looking %on% = onlineplayers()
cmd add looking %pl% = playerinfo(%on%[%num2%])
cmd add looking %found% = \"player\"
cmd add looking if (%pl%[\"x\"] = %xx% and (%pl%[\"y\"] = %yy% or %pl%[\"y\"] = %yy% + 1)) and %pl%[\"z\"] = %zz% then goto 500
cmd add looking %num2% = %num2% + 1
cmd add looking if %num2% != %end% goto 203
Here I restart the loop, unless the command didn't find anything within a range of 100 blocks to prevent infnity loops. It would become infinity if they for example watch the sky or look forward on a flat terrain.
Code:
cmd add looking %num% = %num% + 1
cmd add looking if %num% > 100 then message %p% Nothing found in a range of 100 blocks
cmd add looking if %num% > 100 then exit
cmd add looking goto 100
This is where the command ends if they did find something during the loop. It will just send the coordinates and whether you found a player or a block. If it found a player, the variable %pl% contains the playerinfo of the player you found. If it found a block, %block% contains the blockinfo. From this point you can choose whatever you want to do with the information you found.
Code:
cmd edit looking 499 #Sending results
cmd add looking message %p% \"You're looking at a \" + %found% + \" at [\" + %xx% + \", \" + %yy% + \", \" + %zz% + \"]\"
I didn't realise this post was so long until I looked at the preview... Congratulations on reading until the end and I hope it was useful! If there are any errors let me know, because I didn't have the chance to test it.