Тёмный

(04) Stealth Game - Field of View (Unity, C#) 

AJTech
Подписаться 8 тыс.
Просмотров 25 тыс.
50% 1

[Field of view AI] Ever wanted to make an epic stealth game from scratch and learn while you do so? Well... you've come to the right place, today we'll be working on a field of view system which will allow our AI to detect if it can see our player. We will also be improving our jump to make it more believable and playable... Enjoy. (Made in Unity and C#)
____________________________________________________________
Join me on --
✄ / codewithajay ✄
----- LETS TRY AND HIT : 45 LIKES ✌ ---------
★ Upcoming Videos :
- Building a Self Driving car With Neural Networks ❤
- Development Log #2 ❤
See the Finished Product of todays episode : 0:00 - 0:56
---------------------------------------------------------------------------
Fixing our Jump - 17:43 - 21:44
[More chapter markers down below]
The next part is even better and in order for you to see it you have to subscribe and tap that bell icon to be notified : ru-vid.com_...
Links :
----------------------------------------------------------------------------------------------------
[FILES] Part 4 Project (w/ all scripts + scenes): corneey.com/wsBMOl
[VIDEO] Previous Part (Camera Collision) : • (03) Stealth Game - Ca...
[VIDEO] Part 1 (Character Controller) : • (01) Stealth Game - Cu...
If you enjoyed this video please COMMENT down bellow exactly what YOU would like to see in the next episode and I'll add it in and shout you out!! I need your contribution in order for me to make this channel bigger and better for everyone ;)
----------------------------------------------------------------------------------------------------
If you want to see something next episode, comment down below but if you don't I have some great ideas myself.
IDEAS IN THIS VIDEO ARE FROM :
- 'Grant Christie' : Making the jump fall speed faster over time
- 'Mookfasa' : Decrease popping at the start of jump
Thank you for the suggestions and the support from the community :)
Help make this channel bigger by tapping that like button it would really help me out :) Thank you everyone for your support.
Parts of the Video :
-------------------------------------------------------
What is this about : 0:00 - 0:56
Building the visualisation : 0:56 - 9:15
Writing the FOV Detection : 9:15 - 15:30
Making AI work with FOV : 15:30 - 16:51
Final Test - 16:51 - 17:43
Fixing our Jump - 17:43 - 21:44
Finished Product : 0:00 - 0:56
-------------------------------------------------------
- Video Description -
This video is mainly focused on giving vision to our AI by creating a script which will allow it to detect if it can see the player, we will use some simple maths to detect if the player is in the radius of the AI, then check if the player is in the view angle of the AI then we will make sure there are no walls blocking the sight and after all that we will know that the AI can see the player! :) Then we will also be improving the jump of our character to make it more realistic and fun to use :D
- Series Description -
This series was built to be an entertaining way of learning Unity while making an incredible game, you will learn how to make complex AI behaviour and write complex scripts such as custom character controllers w/ custom handled collision and slopes and Editor Code to control behaviour. All being essential skills to being a Pro Unity Developer.

Игры

Опубликовано:

 

25 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 140   
@Jack-oq7rg
@Jack-oq7rg 5 лет назад
Thank you for this amazing video , allow me to edit your code since most of scenes has lots of colliders , the array will not fit more than 10 objects , if u wanna increase the array size for example 50 this will cause memory leak , the solution is : 1- I've added : public float heightMultiplayer = 1.5f;// this will move the post raycast up. 2- move inFOV to inside fixedUpdate method. public void inFOV(Transform checkingObject, Transform target, float maxAngle, float maxRadius) { Vector3 directionBetween = (target.position - checkingObject.position).normalized; directionBetween.y *= 0; RaycastHit hit; if ( Physics.Raycast(checkingObject.position + Vector3.up * heightMultiplayer, (target.position - checkingObject.position).normalized, out hit, maxRadius)) { if ( LayerMask.LayerToName(hit.transform.gameObject.layer) == "Player") { float angle = Vector3.Angle(checkingObject.forward + Vector3.up * heightMultiplayer, directionBetween); if (angle
@batchprogrammer108
@batchprogrammer108 5 лет назад
Thank you very much for this solution! Would be very helpful :)
@noreasonchannel4012
@noreasonchannel4012 5 лет назад
in fixed Update it gives error,cannot implicitly convert type void to bool. .
@Jack-oq7rg
@Jack-oq7rg 5 лет назад
No Reason Channel , Remove “return true” the method should bot return its void , its only changing the value “isInFoV”
@noreasonchannel4012
@noreasonchannel4012 5 лет назад
@@Jack-oq7rg Hey! Thanks for replying, I've been using your updated code and its shoiwing me error in the fixed update when i assign "isInFOV = inFOV (transform, player, maxAngle, maxRadius);" . . . However if i use the one mentioned in the snippet mentioned in the video, in line " public static bool(...)" ...'not all code paths return a value' error generates... So could you please clarify it? I would like to test and try both methods. Thanks. *Subbing to your channel*
@noreasonchannel4012
@noreasonchannel4012 5 лет назад
This is how the code is working . Hopefully if you find any errors/ leaks, let me know, public bool inFOV(Transform checkingObject, Transform target, float maxAngle, float maxRadius) { Vector3 directionBetween = (target.position - checkingObject.position).normalized; directionBetween.y *= 0; RaycastHit hit; if ( Physics.Raycast(checkingObject.position + Vector3.up *heightMultiplayer , (target.position - checkingObject.position).normalized, out hit, maxRadius)) { if (LayerMask.LayerToName(hit.transform.gameObject.layer) == "Player") // if(hit.target.CompareTag("Player")) { float angle = Vector3.Angle(checkingObject.forward + Vector3.up *heightMultiplayer , directionBetween); if (angle
@khazzor8564
@khazzor8564 5 лет назад
Just wrote an article for university about that :D glad I started from here, yours came out as the best Option
@xdmcreamysickalien9067
@xdmcreamysickalien9067 3 года назад
sucks that this series has ended with only 3 episodes, BUT this tutorial has helped me so fricking much man....thankyou!
@dumbguy1073
@dumbguy1073 3 года назад
simply awesome!!
@zandrewsonnemaker5579
@zandrewsonnemaker5579 6 лет назад
Hey man, we're all definitely waiting on the new stuff! I hope you aren't going to hard and getting overwhelmed, it's okay to limit the scope of these videos so that by the end we have created something substantial and good! I myself have been taking the character controller you've made here and applying some stuff to it for my own sneak functions. Maybe it would be good to make a video setting the scope, setting exactly what kind of character controller we want for exactly what kind of game we are making, so that we can refine to that end. I really hope you aren't getting discouraged because I've been waiting for that new vid for months homie! Cheers!
@Moon-to9se
@Moon-to9se 3 года назад
SOooo gooood!
@Zexorii
@Zexorii 4 года назад
Really great explanation. Thank you very much :)
@sharkgamestudio7630
@sharkgamestudio7630 3 года назад
Realy good vidéo, i sub !
@dex8959
@dex8959 3 года назад
Great channel. Awesome stuff.
@hoapham7389
@hoapham7389 4 года назад
after all the searchimg, i've finally found the enemy vision tutorial easy to implement into my own game! subscribed!!!
@DavidB-rx3km
@DavidB-rx3km 4 года назад
It's a pity it doesn't work ;)
@PizzaRollz1
@PizzaRollz1 3 года назад
@@DavidB-rx3km Finding this out as well.
@lsoares3d
@lsoares3d 6 лет назад
Simple and useful! Thanks, man
@asleepcarpenter1879
@asleepcarpenter1879 2 года назад
Good job! You really helped me
@adamdedubai6585
@adamdedubai6585 6 лет назад
Clear, quick and strong code !! Maybe AJTech is the best .. Awsome job ! Many thanks ... :)
@batchprogrammer108
@batchprogrammer108 6 лет назад
Adam Dedubai Thank you for your kind words Adam, it made my day :)
@diliupg
@diliupg 5 лет назад
very well done! Shorter video, better explained than most!
@batchprogrammer108
@batchprogrammer108 5 лет назад
Thank you very much =D !
@Furukkan5
@Furukkan5 3 года назад
Amazing video! I need this but for 2D topdown... :D I tried adapting your way/code but just couldn't calculate it well lol
@desawwww
@desawwww 3 года назад
Great Tutorial man :D
@karlinthemiddle
@karlinthemiddle 3 года назад
This is an amazing tutorial. Thank you!
@flying.volcano.studio7727
@flying.volcano.studio7727 2 года назад
now FOV is clear! thanks dude
@2ndWord
@2ndWord 4 года назад
Amazing dude, you just saved my ass xD Thanks for the very informative and easy to follow tutorials
@warriorsathletics4343
@warriorsathletics4343 4 года назад
can u link me ur code bc my code is broken and it doesn't work
@nemanja54pchead39
@nemanja54pchead39 4 года назад
Amaizing
@regis5507
@regis5507 4 года назад
thank you, really helped
@HackerFlawless
@HackerFlawless 6 лет назад
Wow, such a cool kid
@GTgamesetutorials
@GTgamesetutorials 4 года назад
I always knew I should't have skipped those math classes back in high school
@ke9n
@ke9n 6 лет назад
P.S. I subscribed anyways :p Never know when you need a fellow dev to talk to/look at something!
@batchprogrammer108
@batchprogrammer108 6 лет назад
Thank you! :) That's very true, keep in touch
@handsomelessking
@handsomelessking 4 года назад
thank you for the tutorial, it didn't work but i somehow jammed it in, wrote some stuff over and it worked
@someone4422
@someone4422 5 лет назад
I subscribed
@batchprogrammer108
@batchprogrammer108 5 лет назад
Thank you so much! :)
@alexcsakfalvi9416
@alexcsakfalvi9416 4 года назад
Hey AJ! You helped me out a lot with this, my only problem is, that i applied these scripts to my enemies, but if there are 10 on more of the enemies, then some of them will not detect the Player prefab.
@michaelgreenhut180
@michaelgreenhut180 4 года назад
Maybe I set this up wrong or missed a step, but I wouldn't call Jump() every frame and then check for a spacebar press, I'd do it the other way around - it might take more setup, but it would be worthwhile. Among other things, the grounded variable is toggling every other frame or so with this setup, and pressing space is therefore ineffective half the time. But I could have forgotten to tweak something. Otherwise, awesome code.
@callumweaver7013
@callumweaver7013 4 года назад
how would i adapt the code to shoot out more rays than 2 that are all in the angle range, as for some reason it only trigger the isInFov when it overlaps with the rays, not inside the angle range.
@TheLeontheking
@TheLeontheking 6 лет назад
this will become the new Assasin's Creed ;)
@batchprogrammer108
@batchprogrammer108 6 лет назад
ancient child Hahah yeah ofcourse 😂
@batchprogrammer108
@batchprogrammer108 5 лет назад
Hey guys, if you are getting an error w/ index out of bounds or anything else try and increase the buffer size of the collisions to 500 (depending on how many items there will be at the same time around the NPC). This error was resolved by @FrostyIceberg
@areebraza8262
@areebraza8262 5 лет назад
but why use a limited buffer size? cant u use like a list? or is that not possible?
@sharkgamestudio7630
@sharkgamestudio7630 3 года назад
but how i do that? I dont know please help me :(
@noxiousbloom1363
@noxiousbloom1363 4 года назад
how can i make the detecion ignore one specific layer or object? thank you for the video!
@tsvetomiriliev5804
@tsvetomiriliev5804 5 лет назад
THANK YOU for this great enemy sight tutorial, this is the forth and only video that I have seen today that really does the job. I believe for performance its better to remove the update, and what I did was OnTriggerStay to execute void InFov(). This way it will execute only when player is near the enemy.
@batchprogrammer108
@batchprogrammer108 5 лет назад
You are absolutely right, that is a good idea as well!
@PakGrantChristie
@PakGrantChristie 6 лет назад
I don't think my last post was saved. I am really keen to see whether you get to implement and over the shoulder aiming function to your camera. While aiming, is it possible to operate your pitch and yaw and movement functions from a behind the players shoulder. Or do you have a better ways to do this :) Keep up the great work AJTech.
@batchprogrammer108
@batchprogrammer108 6 лет назад
Oh wow that's an awesome idea! I have not yet played with these features but I will try my best to implement everything that you have suggested, and yeah maybe it didn't save because I didn't see it sorry. I will upload that as my next episode for sure :D Thanks again for your idea :)
@batchprogrammer108
@batchprogrammer108 6 лет назад
Okay so I played around with this feature and it seems that it would be better to implement this system after we have a player model and I would like to add that after getting a few core features of the game done, so thank you so much for suggesting this and I will certainly be posting on this idea in a few episodes, sorry about that :( Thanks for suggesting though :)
@PakGrantChristie
@PakGrantChristie 6 лет назад
Thanks so much for looking into it and I totally understand. I am using a character in my prototype and have it aiming up and down based on the mouse. I have used a second layer in the animator for the aiming up and down animations using weighting - I switch it to 1 when I am aiming. I initially stopped the camera from controlling the player's rotations using a bool but that meant the player was only aiming up and down but I had no control pointing the player left or right. I thought if I toggled the target position of the camera to an empty game object childed just behind the right shoulder while aiming, that might allow me to keep the camera as the controller of players movement while aiming but alas I am not clever enough to work this out :(. But I will be patient and see how the controller progresses and it is clear we are in very good hand with you AJTech. I have already learn sooo much after 4 episodes so thanks mate :)
@batchprogrammer108
@batchprogrammer108 6 лет назад
Hey Grant, I'm sorry to hear that you are having trouble with this :( I can understand your pain, I initially thought this would be simple to implement until I got into it. I think I may be quite close to getting a solution to this problem. I made a very simple model in Blender with arms and I will be coding a simple Inverse Kinematic solution to make the arms bend and always hold the gun then I should be able to rotate the gun and the player using the Camera Controller script and get a nice looking aiming function. I'm not 100% sure if this will work but the next episode might be a bit delayed so I can get this working properly. This is my first time playing with IK as well so I'm not sure about how well it will turn out, but I hope I can get this working for you :) Glad to hear that you are learning from this series :) Thank you for the support :)
@PakGrantChristie
@PakGrantChristie 6 лет назад
It has been a lot of fun thanks to you. Wow that sounds amazing re IK. I can't wait to see and learn more about that when you are done. :)
@quickstorm8823
@quickstorm8823 5 лет назад
Hey can I ask why you use overlapsphere instead of Distance? I got it to work with distance and seem easier to me.
@batchprogrammer108
@batchprogrammer108 5 лет назад
Of course distance would work as well, but you have to query for certain objects I generalize the problem by checking for all objects within a radius
@dingaloo1056
@dingaloo1056 3 года назад
Hey, quick question for anyone who can answer: isn't it a bad idea to use detection through onDrawGizmos? It was my understanding that this wouldn't run at all in an actual exe of the game, it was just for testing purposes. thanks! edit: I just made an exe of my game using this method, and indeed, the enemy does not detect anything with this method
@Secretforest100
@Secretforest100 5 лет назад
shit this is useful
@shadowgearaudio293
@shadowgearaudio293 5 лет назад
I have been having some issues with the code. For some reason whenever I run the program I get two different problems. The gizmos has a problem that causes an error saying. "Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmos selected UnityEngine.Gizmos.DrawWireSphere" The other problem is whenever the AI sees me it teleports me to it. The only fix I've been able to get is to get an indexoutofbounds error. Am I missing something in the code? I had copied the exact code down to the letter and it still doesn't work.
@ibra.h
@ibra.h 4 года назад
Pretty common sense... You arent drawing gizmos in the ONdrawGizmos() Method
@DJLKM1
@DJLKM1 5 лет назад
Hey Ajay, i have an issue with my project and not really relative to this video sorry, not sure if you can help. The floor prefab tiles im using for indoor flooring are quite small (1x1 ingame meter), the character keeps dipping into the floor while walking across the floor, i thought it was due to the mesh colliders having a tiny gap between them so i made sure it was all snapped right, but its still happening. My second idea was to put a dummy ground plane a hair height under the whole floor, this didnt work either. Any ideas would be awesome.
@batchprogrammer108
@batchprogrammer108 5 лет назад
Are you using the controller that is used in this video? Because if so, there could be a few issues -- are the colliders on the player set up properly?
@DJLKM1
@DJLKM1 5 лет назад
Hi @@batchprogrammer108 Yeah im using your controller and its setup exactly as you have here (up to tutorial #6), i also have found another issue, it to is a collider issue. your controller is totally custom and doesn't rely on any unity control components which is admirable. But unless im missing something stupid; Trigger colliders or trigger zones do not work with your controller as the unity engine is expecting a rigidbody to enter the collider in the OnTriggerEnter methods, so im a bit stuck lol
@batchprogrammer108
@batchprogrammer108 5 лет назад
@@DJLKM1 Oh that is a very interesting problem, I'm not exactly sure what the answer to the previous question is but for this one, one simple work around could just be to add a child object to the player and attach a rigidbody and trigger to it, but this is a workaround. This controller i made is to show people the different features that unity has but if you are building a production level game i highly reccomend building a player controller that implements rigidbodies as they have very high fidelity collision support. I have actually built yet another controller that uses full rigidbodies however i manage things such as gravity and jumping without rigidbodies. Hope this helps :)
@DJLKM1
@DJLKM1 5 лет назад
@@batchprogrammer108 Thanks for getting back, sorry to seem like im throwing mud, your work is awesome. I did add a rigid body to the player as a test, that had no effect sadly. Moving on, you say you have a full RB controller, is there a planned tutorial on that or a link for the project files?. C# and game design is not my full time suit, its just a hobby, just trying to learn and get something going, fulltime im a web designer/programmer. Anyhow keep up this awesome work im enjoying following it all. :)
@batchprogrammer108
@batchprogrammer108 5 лет назад
@@DJLKM1 I have a long break coming up and i would be more than happy to publish a tutorial on that :)
@warriorsathletics4343
@warriorsathletics4343 4 года назад
help my line is red when im not in it but once i go in it turns green and doesnt go back to red once i leave and i get this error ndexOutOfRangeException: Index was outside the bounds of the array. fov.inFOV (UnityEngine.Transform CheckingObject, UnityEngine.Transform target, System.Single maxAngle, System.Single maxRadius) (at Assets/Scripts/fov.cs:44) fov.Update () (at Assets/Scripts/fov.cs:80)
@warriorsathletics4343
@warriorsathletics4343 4 года назад
also once i leave FOV i cant go back in now after i reset unity
@enqu176
@enqu176 3 года назад
You probably have this figured out buy now but im still going to answer anyway. "Index was out of bounds means that the array was too small to get all the gameobjects. I reccomend that you : A : Higher your array size B : Lower the maxRadius value Hope this helps!
@warriorsathletics4343
@warriorsathletics4343 3 года назад
Diov2Void nope I didn’t figure it out but this worked thx
@noreasonchannel4012
@noreasonchannel4012 5 лет назад
HI there, used this code, but error shows in the last process, in line " public static bool(...)" ...'not all code paths return a value' error...
@manuelgraca3118
@manuelgraca3118 3 года назад
dont forget to return some bool value
@HeroSpiderman
@HeroSpiderman 5 лет назад
My code seems to always return false at the end of inFOV for some reason. Any clue?
@batchprogrammer108
@batchprogrammer108 5 лет назад
There must be something wrong with the buffer, please see the other code posted in the comments which have suggested good improvements.
@HeroSpiderman
@HeroSpiderman 5 лет назад
@@batchprogrammer108 will try. Thank you.
@zandrewsonnemaker5579
@zandrewsonnemaker5579 6 лет назад
Hey, sorry to post again. Just went through this code line by line, then out of frustration copying and pasting your code straight over mine entirely, just to figure out why I'm getting an out of range exception on that array. Copying and pasting your code didn't fix it, everything else is working fine. Why am I out of range?
@zandrewsonnemaker5579
@zandrewsonnemaker5579 6 лет назад
Kid you not, literally as soon as I commented the script just started working again. I am positive that I changed not one single letter of code -.- well at least I finally have this implemented.
@batchprogrammer108
@batchprogrammer108 6 лет назад
Zandrew Sonnemaker Hahah at least it’s working 😂
@9898Hawk
@9898Hawk 6 лет назад
I am having the same issue, help!?!!?
@9898Hawk
@9898Hawk 6 лет назад
Nevermind! I fixed the error by increasing the "Collider[] overlaps = new Collider[10]" to "Collider[] overlaps = new Collider[100]." I am using this code for a completely separate project and it was a huge help!
@pious276
@pious276 5 лет назад
Where did you learn all that?
@batchprogrammer108
@batchprogrammer108 5 лет назад
Just putting together different things that you learn along the way
@theminusel5863
@theminusel5863 5 лет назад
public static bool(...)" ...'not all code paths return a value' error... i have a problem i cant fix it what a spend a time.
@manuelgraca3118
@manuelgraca3118 3 года назад
remember to return bool value.. xD
@theminusel5863
@theminusel5863 3 года назад
@@manuelgraca3118 Nah. I don't need this anymore now I can write code with myself
@tristunalekzander5608
@tristunalekzander5608 6 лет назад
I'm a noob and I made double sure I did everything you did but the player isn't being detected. What could I be doing wrong?
@batchprogrammer108
@batchprogrammer108 6 лет назад
Oh okay.. Hmm, that's very difficult because there could be a lot of things that could be going wrong. You can try : Making sure your player has a collider, make sure you're actually calling the 'inFov()' method, but I am sure that you have already done these things. I apologize but I can't really help without an understanding of the situation, maybe you could go to this link - github.com/AJTech2002/Stealth-Series/blob/master/Parts/Stealth%20Game%20Part%204/Assets/Scripts/FOVDetection.cs and just check if the code does really match up, because sometimes the small little mistakes matter. I hope this helps :)
@tristunalekzander5608
@tristunalekzander5608 6 лет назад
Thanks for responding :) I subbed because of it. I couldn't find a difference so I just copied and pasted your code and it still doesn't work. Everything works except the player isn't being detected and inFOV isn't returning true like it should. (Just so you know, I did put the player object into the public player transform and have increased the maxAngle and maxRadius so I can see the gizmos) One other thing, I can't see the gizmos in-game. I thought that was normal but you seem to be able to see them while playing. Idk if that would help you figure out what I am doing wrong. I also didn't watch your other videos in the series, is there another script needed to make this one work or something?
@batchprogrammer108
@batchprogrammer108 6 лет назад
Tristan Thank you for the sub Tristan! Firstly this script is 100% independent so it should work alone, secondly on the topic of rendering gizmos in Game view, that is actually an option you can enable and disable. In your game view look at the option buttons just above where the ‘Maximise view’ buttons are and select the option ‘Gizmos’ and it should draw the gizmos. Now i do have one question that could clear things up, are you using this script in a scene with a lot of game objects? Because we set the collider buffer at 10 and if there are more than 10 objects in the sphere radius before the player enters it may not be detected. My entry and Debug.Log what objects are being detected and what isn’t. If this is the issue just try and increase the collision buffer from 10 to something that suits you. Then if that doesn’t work, just try Debugging every step of the inFov code and see where it stops just to get an idea. Also make sure that you Debug the ‘count’ variable so we can see if it even knows the player is in the radius. If it doesn’t then either increase the buffer size or we have some type of a issue. When i mean buffer size I am referring to this line ‘Collider[] overlaps = new Collider[bufferSize]; . I really hope some of this helps you out, just give me the results of your debugging journey and we’ll try figure this out. Have a great day :)
@tristunalekzander5608
@tristunalekzander5608 6 лет назад
AJTech For some reason it only works in game mode lmao. After I turned on the Gizmos to visible in game mode the red line did indeed turn green when I moved within its range. So it does what I want it to now but now I'm curious why it doesn't work in the scene view? Also, thanks!!!! :D
@batchprogrammer108
@batchprogrammer108 5 лет назад
Hm, so there could be a few issues - The ray could be colliding with the player/object that it is coming out of or maybe the layers haven't been setup properly. Could you create a Debug.Log in the Physics.Raycast & see what object it is actually detecting or is it not detecting anything?
@waynebeatsbeatmaker7450
@waynebeatsbeatmaker7450 3 года назад
Can i contact you pls? i having troubles to implement this on my game. the error: IndexOutOfRangeException: Index was outside the bounds of the array. LINES " if (overlaps[i] != null) " && lines " isInFov = inFov(transform, player, maxAngle, maxRadius); "
@creeper20ps
@creeper20ps 3 года назад
Its giving an error because the array is too much smaller than the colliders that its detecting. The array in that script is: Collider[] overlaps = new Collider[10]; That Wireframe yellow sphere. Inside that, it'll detect any object that has a component of a "Collider". So let say.... you have the floor and the player..., that's two colliders. If you go beyond 10 colliders inside the yellow sphere. It'll give you an error. So plan it out. If you think there's going to be like 30 object or more inside that yellow sphere, that has a component collider. Change This: Collider[] overlaps = new Collider[10]; To this: Collider[] overlaps = new Collider[40];
@waynebeatsbeatmaker7450
@waynebeatsbeatmaker7450 3 года назад
@@creeper20ps Acabei entendendo sua explicação, é que como os codigos passados são em inglês, eu acabei não pegando a referencia do " new Collider [10]" ser a quantidade de colliders dentro do meu array, agora eu resolvi, agradeço o suporte, greetings from brazil.
@garydarc822
@garydarc822 3 года назад
@@creeper20ps Dude you're legend, thank you!
@hejter4177
@hejter4177 6 лет назад
IndexOutOfRangeException: Array index is out of range. Detector.inFOV (UnityEngine.Transform checkingObject, UnityEngine.Transform target, Single maxAngle, Single maxRadius) (at Assets/scripts/Detector.cs:47) Detector.Update () (at Assets/scripts/Detector.cs:88) cs47 if (overlaps[i] != null) cs88 isInFov = inFOV(transform, player, maxAngle, maxRadius);
@batchprogrammer108
@batchprogrammer108 6 лет назад
Hejter In the for loop inside the inFov() method just try and remove the +1 near the count. So instead of i
@user-ck1ij3pn1t
@user-ck1ij3pn1t 4 года назад
Hey has anyone played Spider-Man shattered dimensions I know Spider-Man is childish but the noir levels are amazing so does anyone want to make a game like the noir levels but a fully fleshed out game? If so I will update this comment soon to add a way to contact me but it will be soon I will wait to see if anyone actually wants to make the game so check back every so often
@salmanhayat6262
@salmanhayat6262 5 лет назад
not worked for me
@artinmajidi1654
@artinmajidi1654 4 года назад
public float maxRadius; public float maxAngle; public Transform player; private void Update() { if( inFOV(Enemy, player, maxAngle, maxRadius) == true) { is_inFOV = true; } else { is_inFOV = false; } } private void OnDrawGizmos() { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, maxRadius); Vector3 fovLine1 = Quaternion.AngleAxis(maxAngle, transform.up) * transform.forward * maxRadius; Vector3 fovLine2 = Quaternion.AngleAxis(-maxAngle, transform.up) * transform.forward * maxRadius; Gizmos.color = Color.blue; Gizmos.DrawRay(transform.position, fovLine1); Gizmos.DrawRay(transform.position, fovLine2); if (is_inFOV == true) Gizmos.color = Color.green; else Gizmos.color = Color.red; Gizmos.DrawRay(transform.position, (player.position - transform.position).normalized * maxRadius); Gizmos.color = Color.black; Gizmos.DrawRay(transform.position, transform.forward * maxRadius); } public static bool inFOV(Transform checking_object, Transform target , float maxAngle, float maxRadius) { Collider[] overlaps = new Collider[50]; int count = Physics.OverlapSphereNonAlloc(checking_object.position, maxRadius, overlaps); for (int i = 0; i < count + 1; i++) { if(overlaps[i] != null) { if(overlaps[i].transform == target) { Vector3 direction_between = (target.position - checking_object.position).normalized; direction_between.y *= 0; float Angle = Vector3.Angle(checking_object.forward, direction_between); if(Angle
@MrGidorMr
@MrGidorMr 4 года назад
@@artinmajidi1654 not works
@DavidB-rx3km
@DavidB-rx3km 4 года назад
=== Just a note for new people, this script === NO LONGER WORKS=== I'm not sure if it ever worked in the first place Is anyone not getting the green line to activate within Unity. I was trying on my own stuff and it didn't work, thinking it was something to do with the transforms etc of my 'object', but I created a new scene and used two cubes and it still doesn't work? I've spent three days trying to get to the bottom of it. (also the links to the "[FILES] Part 4 Project" no longer work). i.imgur.com/y8oblRR.jpg
@DavidB-rx3km
@DavidB-rx3km 4 года назад
Just to update, I tried this on a fresh project, new scene on a different PC, and it no longer works (green line doesn't appear) on 2018.3. Is anyone else having trouble with it?
@Tommy-rt7sq
@Tommy-rt7sq 4 года назад
@@DavidB-rx3km it works...? just tried it now.
@supremederpy634
@supremederpy634 4 года назад
doesnt work for me aswell ...
@ncox27
@ncox27 4 года назад
I was able to get it to work on my project as well Unity 2019.3.9f1. I'm trying to now modify it to work along the X-axis as well to make more of a cone view so If anyone has a way to do that I'd love to hear it. @Davers if you're still interested share your code and I'll compare it with mine.
@warriorsathletics4343
@warriorsathletics4343 4 года назад
@@ncox27 how did you get it to work bc when i enter the FOV the line turns green but when i leave FOV the line stays green and i get this error indexOutOfRangeException: Index was outside the bounds of the array. fov.inFOV (UnityEngine.Transform CheckingObject, UnityEngine.Transform target, System.Single maxAngle, System.Single maxRadius) (at Assets/Scripts/fov.cs:44) fov.Update () (at Assets/Scripts/fov.cs:80)
Далее
(05) Stealth Game - Inverse Kinematics pt1
1:08:54
Просмотров 3,9 тыс.
Field of view visualisation (E03: stencil shader)
6:44
Выпускаем трек? #iribaby
00:14
Просмотров 494 тыс.
I tried 'Suicide Squad' Season 2...
27:00
Просмотров 18 тыс.
STEALTH ENEMY AI WITH UNITY AND C# - EASY TUTORIAL
11:40
THIRD PERSON MOVEMENT in Unity
21:05
Просмотров 1,4 млн
Field of view visualisation (E01)
22:32
Просмотров 437 тыс.
Field of view visualisation (E02)
27:23
Просмотров 158 тыс.
Making a First Person Shooter with Thomas Brush!
4:50
How To Build An Event System in Unity
8:01
Просмотров 408 тыс.
Better Jumping in Unity With Four Lines of Code
12:47
Просмотров 819 тыс.
ЕА Показали ГЕЙМПЛЕЙ FC 25...
10:35
Просмотров 162 тыс.
skibidi toilet multiverse 039 (part 4)
6:06
Просмотров 4,2 млн