2D Field of View [Unity Tutorial]

Поделиться
HTML-код
  • Опубликовано: 18 дек 2024

Комментарии • 52

  • @emredesu
    @emredesu 2 года назад +20

    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) < angle / 2) { ... }
    // 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));
    }

  • @jomama55ful
    @jomama55ful 2 года назад +14

    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 2 года назад

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

    • @jomama55ful
      @jomama55ful 2 года назад +1

      @@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 2 года назад

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

    • @neozoid7009
      @neozoid7009 2 года назад

      @@jomama55ful how can i get your git project

  • @perfilcomentarista2922
    @perfilcomentarista2922 2 года назад +5

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

  • @nickgennady
    @nickgennady 2 года назад +3

    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.

  • @krissloo143
    @krissloo143 3 года назад +1

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

  • @personalgamedevyt9830
    @personalgamedevyt9830 Год назад

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

  • @stevancosovic4706
    @stevancosovic4706 2 месяца назад

    Dude your great, thanks!

  • @Elenyen
    @Elenyen Год назад +1

    Great tutorial :)

  • @wickedpichu571
    @wickedpichu571 Год назад

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

  • @ElizabethStripes-nk8ww
    @ElizabethStripes-nk8ww 7 месяцев назад

    thank you so much you saved my life with this vid

  • @jomama55ful
    @jomama55ful 2 года назад +3

    "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.

  • @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

  • @GustOfLuck
    @GustOfLuck 2 года назад

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

  • @SMBJS
    @SMBJS 2 года назад +3

    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 года назад +1

      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 2 года назад

      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.

  • @fireblastodvr
    @fireblastodvr 3 года назад

    Keep Up The Good Work!

  • @dragonballz3686
    @dragonballz3686 2 года назад

    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.

  • @KarasawaL30
    @KarasawaL30 2 года назад

    Cheers, excellent tutorial! Definitely subbing for more

  • @Restart-Gaming
    @Restart-Gaming 2 года назад

    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?

  • @drbrinkki
    @drbrinkki 2 года назад

    I love the accent.

  • @pablosuarez9211
    @pablosuarez9211 2 года назад +1

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

  • @forcesoftheevil9252
    @forcesoftheevil9252 2 года назад

    Thanks 4 tutorial! It very usefull

  • @davidbarnaba7229
    @davidbarnaba7229 2 года назад +1

    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.

  • @dak5327
    @dak5327 Год назад

    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.

  • @jud.su.5developer895
    @jud.su.5developer895 2 года назад

    You are great 😊

  • @noltarferior
    @noltarferior 2 года назад

    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

  • @Deewens
    @Deewens 2 года назад

    Thanks you, the script is working well!

  • @ItsMeHelel
    @ItsMeHelel 2 года назад

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

  • @griffon2_312
    @griffon2_312 2 года назад +1

    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

  • @josemartinfriasaguirre1591
    @josemartinfriasaguirre1591 2 года назад

    15:00 Lmaooo; also, thanks for the video

  • @Pablete257
    @Pablete257 2 года назад

    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
    @SurreyMuso 2 года назад +1

    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.5developer895 2 года назад

    1:53 😋🤣

  • @neozoid7009
    @neozoid7009 2 года назад

    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

  • @tomkiptom
    @tomkiptom 3 года назад

    🐕

  • @entertainmentoverloaded5700
    @entertainmentoverloaded5700 3 года назад

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

    • @comp3interactive
      @comp3interactive  3 года назад +10

      Love a good comment which doesn't outline anything, all comments are always good for RUclips 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!