Hey guys follow fear and today we’re going over a very important topic I wanted to make a separate video for this I’ve covered this before but it was in its own kind of creation thing but we’re going to be covering how to detect when a player is looking at something a block An entity a location I don’t know and we’re going to cover two methods one is going to be the one that you can do without a data pack it’s really fast but less efficient I mean but less accurate but it’s also way more efficient and then we’re going to go over the Raycasting method which requires a data pack but it’s like super duper accurate easy to tune to the right values my name is so we’re going to go over the first one because it’s very interesting this is one that I saw posted on the MCC resources and I’ve tweaked it a little Recently to create some interesting stuff that I’ll be showing off in the next video that has to do with hitbox detection but this is detecting if you’re facing something so essentially as you can tell when let me deactivate this when I look somewhat towards the direction of the armorstand it starts Glowing and then when it’s out of my view it stops glowing and it’s not perfect because you can see the how the detection only works when I look somewhere near the center of its body so and then a little bit above its body as well so there is some offset Issues that you can probably mess with but it’s kind of hard to tune this one but it does work in just one command and this is the long long command so I’m going to pull it up inside a its own file and a function file so you can see The full command better and how it works is kind of interesting so let’s go ahead and do this as look alright so this is kind of long so we’re gonna have to go over it slowly step by step it’s a very big execute all right so essentially I’m Going to use some blocks to over-exaggerate what’s happening so the player is gonna be right in the center it’s the white okay so it occurs to the player’s eyes which just makes sense you’re looking where the players eyes are and you’re going to face the execute command towards this entity now you can Change this at E to be whatever entity you want to detect if they’re looking at like usually I would do like an entity with a tag called like face me or something all right and you’re going to look towards that entity’s eyes okay then you’re going to anchor to its feet And you’re going to move to sorry anchor to your feet and then move in the direction of that entity you can also remove this anchored feet to change the elevation and stuff and mess with some of these values to change where the hitbox detects it anyways so you can Moves one block towards it so we’re going to exaggerate by moving like five blocks towards okay all right so then it rotates as the person who’s playing the command which is just the original player okay so let’s say I’m looking this direction right so now it rotates The face the command over here starts off by facing that it moves forward then it rotates to look at the direction I was looking right alright and then it moves backwards in the direction I’m facing that’s what these three carrots mean the third one means the direction you’re facing forward or backwards and Negative 1 means backwards so if I’m facing this way it moves backwards by the same amount of units so that’s approximately four blocks because triangles and it ends up over here right so then it says if you can find the player within zero point three blocks Then you are looking at it so this is where you start to think wait what how does this constitute as looking at it how does this work some of you might have already gotten it so let’s do some hypotheticals so it’s good to when you have some like interesting math Equations you don’t understand it’s good to think about the extremes so let’s think about these dreams and I was looking at it and I played this what would it do it would move forward throughout facing it it would rotate as me where I am looking the same direction As the entity and then it would move back the exact same amount of blocks and what would it be it would end up right where I’m standing so of course it would find me I am looking at it that’s true okay now let’s do the second extreme Where I’m looking directly away from it all right so it moves towards it then it turns around because that’s the way I was facing and then it moves backwards this does will the selector be able to find me hell no I’m Way more than 0.3 blocks Away so I’m definitely not looking at it and we can also go into the near ranges to test near situations so if I’m like this so it would move forward and then it’s like this and then it would move backwards and when it’s moving backwards it would be something like this right And this would be within the point three blocks range considering that we call five blocks one block right so that would be close enough to pick it up so that’s kind of how it works it uses two vectors one vector facing the entity and then it does thoth tail to toe method Where you connect the head of one vector to the tail of the other and moves backwards and you end up with a net displacement of some value so if I did this with say a triangle then if I did it while I was looking 90 degrees Because 180 is kind of extreme then it would move forward like this and then it would move backwards like this one two three four five approx and then the net displacement from the player is this line right here so we’re basically just checking that if the distance of from This end point to the player is within a certain amount if this is small enough hypotenuse is essentially what it’s saying so hopefully that some of you could understand that but if you don’t understand that’s all right all you need to know is you change this value to make It the bigger the value the easier it is to see if the player the bigger the angle is so if I make this one then basically anything in front of me will be picked up and then the other value you can change is who it’s looking at Which is right here and you can also change the facing to be a block so that’s where the do it with blocks comes in it’s literally the exact same command except it’s checking if you’re facing you try and you face a specific coordinate so then it works with this Block now you can actually make this value way more small so you can make it point one to make it way more accurate but again the distance also comes into play so keep in mind that this works from infinite distance away and the further I am away the less accurate I Have to be because it creates an invisible cone and this is where a conic selection comes in and I’ll come into that in my next video but there are some ways we can manipulate it to limit the distance using a couple tricks but anyways so that’s how to detect if you’re looking At something roughly but not accurately and it’s hard to control so the next one hopefully I didn’t take too long is how to do it with Ray casting so we’re gonna go back into this look this is going to be a ray casting command now so I cover A covered ray casting before you’re basically playing the exact same command but in a different location over and over and over again so we just need to do execute positioned one so this means one block in front run a function test look alright so we’re gonna add Particles to this though so we’re gonna do a flame particle or a crit particle and only one crit particle so that way we can see what’s going on so if we do function test look you can see boom it drew a line from my feet but it used the Max command limit of 65 536 so what this does is I am looking this way the positioned moves one block forward in the direction I’m looking and then it plays itself again so when it plays itself again it does the particle it moves again one more forward and one More forward and one more forward one more forward that’s generally how ray casting works now we have to add some criteria here because again you don’t want this to do sixty five five thirty five so you want to check if the block that you’re going to end up in is err That way it plays particles right up until it hits a wall or it encounters the world border because like if it goes outside you render chunks it’s not considered air so that’s only four forty nine the next thing you’d want to do is whenever you play it you want to do Anchored to the eyes just like that anchored with an end so now it’ll come out of your eyes which makes more sense all right so the next adjustment is we have to create some kind of hitbox to detect when you look at something or when you look at a block So one thing you can do if you’re just doing it for a block you can do execute if block and I would do one forward this is where the block versus entity comes into play if you’re checking if they’re in a block you would change this thing to be here That way your raycast goes one block into the block so if you’re currently in there move forward one play one more time this would make you go one inside a block so that you can say if the block you’re in is and let’s get these coordinates What was it okay so if you’re inside this block run set block grey glazed terracotta so we’re just going to use the same pink glazed terracotta so you’re checking if the block is there you can also do some other stuff to check what block you’re in but it’s very simple Terra Terra Cotta okay so this will just basically say if you’re inside a terracotta block sorry if you’re inside a terracotta block just like that then set the block to grey glazed terracotta and if you wanted to do like a specific coordinate you probably have to do like some in an area Effect cloud and check what the coordinates are on it and if the coordinates are those coordinates but anyways so if I run that let’s just go ahead and do you activate this and let’s copy these execute as a s anchored eyes run function test look okay and then This one should be the same so now you’re gonna see a constant line that’s being drawn and it’s laggy because I made it infinite length until you hit an on-air block it’ll go one block into that block so you can see it kind of goes one inside it it’s kind of hard to Tell though because it’s moving so fast but it’s not picking up the pink glazed terracotta okay sorry I had to cut for a sec the position value needs to be less than one otherwise there’s a chance that you step over it because you like jump From the edge of the block to the other side of the block so I go ahead I went ahead and adjusted that and again these are values that are really easy to adjust you just change the position to change the accuracy but it also decreases the efficiency because you Play a lot more stuff but you’ll see that we’ll turn it as soon as I’m looking at it perfectly when I’m looking at it Oh like perfect hit boxing detection for the block so this is very perfect the very inefficient so some of the things you can do to fix this is you Can add a scoreboard called range so you can do scoreboard players remove at s range one and then you can check if score at s range matches one and before you play it let’s see if range exists the scoreboard objectives ad range dummy so before you play it you can say scoreboard players Set at a range 20 okay so then it will count down the range so the line will be much shorter and more efficient there therefore because it will play less commands when you’re looking out into the distance so 20 to calculate how far it’ll travel you basically just have 20 Times 0.5 so 20 times 0.5 is 10 so it’ll travel ten blocks approximately and I like to make the range like five blocks or less because that’s how far the reach of the player is but you can do whatever you want now if you want to detect entities that’s easy There’s a secret trick here that I’m going to show you that I’ll cover a little bit more in depth in the hip box video but there is a perfect hitbox for entities so you do as e DX equals zero positioned down down down if entity s DX equals zero Run effect gave is glowing this is just the fact if something is just looking at a general entity so then let me just add this command here and now we can accurately test this so you can see it works very well and if I remove the correct particle it’s even better than The other has to look how accurate it is I have to touch the actual model essentially for it to trigger of course armor stands are skinnier than you think but it pretty much have to touch the actual model for it to trigger and the interesting thing about this is it works Perfectly on anything so the villager you can see it just bear it only activates when I touch him and you can see a spider it only activates when I touch him so this is probably the best system and to prevent things from going through you could just do you could give Like a tag to this entity and then you can say like if nothing unless something has the tag keep doing the right cast and one closing thought is one if you’re using data packs for this not those command blocks there you’re probably gonna want to have like a function Before it right that says start and this function will do those things that I had before well what you can do is you can do tag add s add test this and then tag at us remove this right and then you can play the raycast which would be this Function okay now the thing that you may get confused and I don’t want to see this in the comments because I’m discussing it now is we say as right here so now we don’t know who shot the raycast right because as is no longer the player it is this random entity The spider at the end of this command if you play say a function right so inside this function as is no longer referring to the player and at is no longer referring to where the player is so you don’t know who what player shot it multiplayer friendly gun well that’s What these solution is for so now you know who the player is because it’s an entity with a tag of this and you know as long as as long as you don’t play another raycast in the sub function you know that any time that you say tag equals this it’s referring to the Original person who played the right cast so that’s just like a shortcut to keep track of who’s you but hopefully that went over enough for you guys to get some good understanding of how to detect when you’re looking at things this is by far the best system that I Use in all my stuff then there’s also the shortcut if you don’t like data packs anyways if you thought this was useful don’t detect yourself anyways thanks for watching and I’ll see in the next one peace Video Information
This video, titled ‘How to Detect Where the Player is Looking in Minecraft’, was uploaded by Cloud Wolf on 2020-03-30 16:41:41. It has garnered 68217 views and 1682 likes. The duration of the video is 00:16:13 or 973 seconds.
We cover how to detect when a player is looking at an entity or block using a command block and raycasting method.
💡 1 Command Code: execute as @p at @s anchored eyes facing entity @e[sort=nearest,limit=1,type=!player] eyes anchored feet positioned ^ ^ ^1 rotated as @s positioned ^ ^ ^-1 if entity @s[distance=..0.3] run effect give @e[type=!player,sort=nearest,limit=1] glowing 10 10 true 💡 Raycasting Command: execute if score @s range matches 1.. if block ~ ~ ~ air positioned ^ ^ ^0.5 run function test:look 💡 Raycasting Hitbox: execute as @e[dx=0] positioned ~-0.99 ~-0.99 ~-0.99 if entity @s[dx=0] run effect give @s glowing 1 1 true
📝New Challenge: Easy ➡️ Make creepers explode when you look at them for too long.
⚔️ Server IP: cloudgames.ml 🔵 Discord Server: https://discord.gg/HyZPCTu 💻 Website: https://dtdeadpool.wixsite.com/cloudgames 🐺 Twitter: https://twitter.com/cloudwolfbane 📧 Email: [email protected]
Music: “Werq” Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 4.0 License
All Songs Used under fair copyright. Lincenses Below: License for commercial use: Creative Commons Attribution 4.0 Unported “Share Alike” (CC BY-SA 4.0) License. Full License HERE – https://creativecommons.org/licenses/by-sa/4.0/legalcode