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)); }
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.
@@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.
"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.
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
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!
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.
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.
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?
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.
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!
@@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!
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));
}
Thanks a lot mate
@@davidserranodelarosa1901 Glad it helped someone!
^
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.
Wow... thanks dude. I really dunno how your comment didn't get any praise.
this also didn't worked for me still line gizmos are incorrectly drawn please help .
@@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.
@@jomama55ful thanks . Ok I will try your and tell you how it goes
@@jomama55ful how can i get your git project
this was the only versatile way of doing it in 2D that I found
thank you so much man
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.
Haven't seen from your channel in a while...
It's as awesome as I last remember it
I watched your 3D video first and attempted to convert it into a 2D one. Thank you for giving videos on both!
Dude your great, thanks!
Great tutorial :)
Thank you so much ❤ This is exactly what i was looking for ❤ ❤ ❤
thank you so much you saved my life with this vid
"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.
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
THX A LOT DUDE u helped me a lot, u have noooo ideia, just thx bro
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!
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.
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.
Keep Up The Good Work!
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.
Cheers, excellent tutorial! Definitely subbing for more
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?
I love the accent.
anyone know how to visualize the field of view in game instead of the inspector?
Thanks 4 tutorial! It very usefull
Nice Tutorial. How may I rotate the cone of view?
I posted a comment to correct the gizmo, so it properly tracks the rotation of the transform. Check that out.
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.
You are great 😊
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
Thanks you, the script is working well!
I have the same question that SM[BJS] has!
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
15:00 Lmaooo; also, thanks for the video
Can you detect if the mouse position is in the field of view?
I believe that if your pointer is a GameObject with a collider and on the right Layer, it should, yes.
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!
Lol. Usage of a Mac vs Windows vs Linux box is a can of worms I don't think should be opened.
1:53 😋🤣
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
🐕
very bad approach of doing....u are a beginner or what!
Love a good comment which doesn't outline anything, all comments are always good for RUclips SEO though 😉
@@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!