2D Field of View [Unity Tutorial]

In this video ,we're going to be taking the 3D Field of View system from my previous tutorial and converting it to 2D! Good huh?
Join me and learn your way through the Unity Game Engine, the C# language and the Visual Studio editor. Remember, if this video was useful then DROP A LIKE! 👍
💯 Want to help me out and allow me to keep making these tutorials? Consider supporting the channel on Patreon:
/ comp3interactive
🤩 SPONSORED LINKS:
www.gigatank3000.com
/ gigatank3000
💬 Join the Discord community here:
/ discord
📱 Follow me on Twitter:
/ comp3int
📱 Play my games:
PC/Mac/Linux: comp3interactive.itch.io/
Google Play: bit.ly/2TisAQo
#UnityTutorial #Unity3D #Comp3interactive #GameDev
0:00 Intro
1:10 Overview
2:20 Writing the Scripts
11:00 Failed Example
11:30 Setting up the Layers
12:20 Working Example
12:53 Drawing a Custom Gizmo
17:26 Outro

Пікірлер: 51

  • @perfilcomentarista2922
    @perfilcomentarista2922 Жыл бұрын

    this was the only versatile way of doing it in 2D that I found thank you so much man

  • @emredesu
    @emredesu Жыл бұрын

    Great tutorial! I'm making a platformer style game where I rotate my enemies around the Y axis 180 degrees (rather than the Z axis), so I made the following adjustments to the code in case it will help anyone else: // To check if the target is within the defined angle - Edit line 42 at 16:46 if (Vector2.Angle(transform.rotation.y == 180 ? -transform.right : transform.right, directionToTarget) // To draw the angles - Edit lines 63 and 64 at 16:46 Vector3 angle01 = DirectionFromAngle(-angle / 2); Vector3 angle02 = DirectionFromAngle(angle / 2); // To get direction from angle - Edit lines 77 to 82 at 16:46 private Vector2 DirectionFromAngle(float angleInDegrees) { return (Vector2)(Quaternion.Euler(0, 0, angleInDegrees) * (transform.rotation.y == 180 ? -transform.right : transform.right)); }

  • @davidserranodelarosa1901

    @davidserranodelarosa1901

    Жыл бұрын

    Thanks a lot mate

  • @emredesu

    @emredesu

    Жыл бұрын

    @@davidserranodelarosa1901 Glad it helped someone!

  • @amazeinggames

    @amazeinggames

    Жыл бұрын

    ^

  • @nickgennady
    @nickgennady Жыл бұрын

    A quick tip. It’s much more optimized to multiply by 0.5 than divide by 2. It will make a difference if you have 10s of thousand of entities.

  • @personalgamedevyt9830
    @personalgamedevyt9830 Жыл бұрын

    I watched your 3D video first and attempted to convert it into a 2D one. Thank you for giving videos on both!

  • @krissloo143
    @krissloo1432 жыл бұрын

    Haven't seen from your channel in a while... It's as awesome as I last remember it

  • @jomama55ful
    @jomama55ful2 жыл бұрын

    Good tutorial. I implemented this and had an issue with the gizmo. I noted a couple of issues that prevent the gizmo from properly tracking the transform as it is rotated. at 16:48, On line 63, remove the - sign in front of "transform.eularAngles.z". this angle is absolute z rotation of the transform and does not need to be negated. Also on line 79, you use "angleInDegrees += eularY". It should be -=. z rotation in the clockwise direction is actually negative. Make these two corrections, and the FOV Gizmo properly tracks the transform's z rotation.

  • @KusanajiKei

    @KusanajiKei

    2 жыл бұрын

    Wow... thanks dude. I really dunno how your comment didn't get any praise.

  • @neozoid7009

    @neozoid7009

    Жыл бұрын

    this also didn't worked for me still line gizmos are incorrectly drawn please help .

  • @jomama55ful

    @jomama55ful

    Жыл бұрын

    @@neozoid7009 Without knowing how they are being incorrectly drawn, I'm not sure what to tell you. I have sample code on my Github project you could look at... PM me and I can point you in the right direction.

  • @neozoid7009

    @neozoid7009

    Жыл бұрын

    @@jomama55ful thanks . Ok I will try your and tell you how it goes

  • @neozoid7009

    @neozoid7009

    Жыл бұрын

    @@jomama55ful how can i get your git project

  • @fireblastdev4407
    @fireblastdev44072 жыл бұрын

    Keep Up The Good Work!

  • @wickedpichu571
    @wickedpichu5717 ай бұрын

    Thank you so much ❤ This is exactly what i was looking for ❤ ❤ ❤

  • @ElizabethStripes-nk8ww
    @ElizabethStripes-nk8ww2 ай бұрын

    thank you so much you saved my life with this vid

  • @KarasawaL30
    @KarasawaL302 жыл бұрын

    Cheers, excellent tutorial! Definitely subbing for more

  • @adamhyland4449
    @adamhyland4449 Жыл бұрын

    Exactly what I needed great tutorial fr a beginner like me, even helped me solve a problem with another script of my own thanks to what I learnt from this

  • @darkeinhardt3760
    @darkeinhardt37602 жыл бұрын

    THX A LOT DUDE u helped me a lot, u have noooo ideia, just thx bro

  • @Elenyen
    @Elenyen Жыл бұрын

    Great tutorial :)

  • @forcesoftheevil9252
    @forcesoftheevil92522 жыл бұрын

    Thanks 4 tutorial! It very usefull

  • @Deewens
    @Deewens2 жыл бұрын

    Thanks you, the script is working well!

  • @jud.su.5developer895
    @jud.su.5developer8952 жыл бұрын

    You are great 😊

  • @dickvanthoff
    @dickvanthoff Жыл бұрын

    I made the mistake of starting a function/void within the - if (CanSeePlayer) - of the OnDrawGizmos void instead of the FOV void. The void is called upon as wanted when testing in Unity, but not in a build (I've only made a WebGL build so maybe Gizmos particularly don't work in that framework?) If anyone else made the same mistake and doesn't understand why it's not working, now you know how to fix it 😋 And thank you very much for this tutorial, I was trying to make other methods work that aren't even eligible for what I needed and your solution just works perfectly!

  • @jomama55ful
    @jomama55ful2 жыл бұрын

    "return new Vector2(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad ), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad))". Essentially, this is using the Pythagorean theorem to complete the square. Sin is used on the X axis, and cosin is used on the y axis. this provides a tangent that gives us our right angles to solve for the angle of the triangle formed by x and y on the surface of a circle. MathF.Sin and Cos require radians, so the angle is multiplied by Pi/180 (Mathf.deg2Rad) . Anyone please correct me if I got any part of that wrong.

  • @josemartinfriasaguirre1591
    @josemartinfriasaguirre15912 жыл бұрын

    15:00 Lmaooo; also, thanks for the video

  • @Restart-Gaming
    @Restart-Gaming2 жыл бұрын

    Might have ask before I bought a unity asset of first person arms with weapons and animation also bought a few maps do you have any videos on how to use what I bought to setup first and third person multi player game set up?

  • @hanzspecter203
    @hanzspecter2032 жыл бұрын

    hello! I ran into a problem which when I hit play the player ref is automatically set to none again, I don't know why

  • @SMBJS
    @SMBJS2 жыл бұрын

    Hi there, thank you so much for this, it’s helped out a ton and it’s better than all the other tutorials out there! Quick question, how do we go about rotating the the arc around the centre of the object? For example, the arc is currently set north, how would I set it to look east when the enemy is looking east? Thanks in advance!

  • @jomama55ful

    @jomama55ful

    2 жыл бұрын

    I commented with a fix that deals with that issue. look for my comment. The FOV code works as intended. The Gizmo does not properly represent what is happening with the FOV.

  • @emredesu

    @emredesu

    Жыл бұрын

    Edit the first parameter of Vector2.Angle at 16:46 on line 42 to whatever you want. For up: transform.up For down: -transform.up For right: transform.right For left: -transform.left To reflect this change on the gizmos drawn, see my comment and replace the transform.right with whatever you're using.

  • @dak5327
    @dak53279 ай бұрын

    does anyone know how to make the FOV rotate with the attached gameobject as it rotates too? (e.g. enemy rotates to look at player and the FOV follows) Because right now my sprite rotates to look at the player but the FOV (yellow lines) always at a 90degree angle to the left of the sprite no matter what.

  • @pablosuarez9211
    @pablosuarez9211 Жыл бұрын

    anyone know how to visualize the field of view in game instead of the inspector?

  • @dragonballz3686
    @dragonballz36862 жыл бұрын

    Bro, how to make a set of enemies coming in a straight line or forming sine wave or in a circle formation like space shooter games.

  • @drbrinkki
    @drbrinkki Жыл бұрын

    I love the accent.

  • @davidbarnaba7229
    @davidbarnaba72292 жыл бұрын

    Nice Tutorial. How may I rotate the cone of view?

  • @jomama55ful

    @jomama55ful

    2 жыл бұрын

    I posted a comment to correct the gizmo, so it properly tracks the rotation of the transform. Check that out.

  • @ItsMeHelel
    @ItsMeHelel2 жыл бұрын

    I have the same question that SM[BJS] has!

  • @Pablete257
    @Pablete257 Жыл бұрын

    Can you detect if the mouse position is in the field of view?

  • @JC-yx7yu

    @JC-yx7yu

    Жыл бұрын

    I believe that if your pointer is a GameObject with a collider and on the right Layer, it should, yes.

  • @SurreyMuso
    @SurreyMuso2 жыл бұрын

    Wow. You've improved on the earlier video in three very important ways. (1) Correct pronunciation of Euler 😀 (2) Use of OnDrawGizmos (so much easier than an Editor script) (3) You're using a Mac. You've clearly taken the red pill. Welcome to the light!

  • @personalgamedevyt9830

    @personalgamedevyt9830

    Жыл бұрын

    Lol. Usage of a Mac vs Windows vs Linux box is a can of worms I don't think should be opened.

  • @jud.su.5developer895
    @jud.su.5developer8952 жыл бұрын

    1:53 😋🤣

  • @griffon2_312
    @griffon2_312 Жыл бұрын

    Hi, I have a question, so I am making the ai units use this method but how do I find the closest object in the layermask with this method

  • @tomkiptom
    @tomkiptom2 жыл бұрын

    🐕

  • @neozoid7009
    @neozoid7009 Жыл бұрын

    Heck yah I also don't have a clue how the math function works . if any body have an explanation please reply . God bless you

  • @entertainmentoverloaded5700
    @entertainmentoverloaded57002 жыл бұрын

    very bad approach of doing....u are a beginner or what!

  • @comp3interactive

    @comp3interactive

    2 жыл бұрын

    Love a good comment which doesn't outline anything, all comments are always good for KZread SEO though 😉

  • @jomama55ful

    @jomama55ful

    2 жыл бұрын

    @@comp3interactive Indeed, even bad comments are good comments for the almighty algorithm. with that said, here is another comment. Please note my comments on corrections to the gizmo, and an explanation of the Mathf functions. Thanks for the tutorial!