Advanced AI in Unity [Tutorial] - Physics, Pathfinding, Editor Adjustments

preview_player
Показать описание
Today in this tutorial we create a fully functional AI system for 2D. This includes jumping, movement, activation and disactivation, and uses the A* algorithm. All of this is programmed in Unity and requires a basic knowledge of programming and Unity usage.

Рекомендации по теме
Комментарии
Автор

8:00 you need to select 2D physics and to also select your Obstacle Layer mask if anyone is wondering.

TheScorpionAly
Автор

A couple small improvements I've made:

even the new version of isGrounded which they have used allows the enemy to fly if there is any type of background which uses a collider because the raycast is able to hit that background and thinks the enemy is grounded. Additionally, the enemy will be unable to jump if it is too close to an edge because the raycast goes down from the centre of the enemy (meaning if less the half of it is on land the raycast will not collide with the ground and you will not jump.)

Corrected line:
RaycastHit2D isGrounded = Physics2D.BoxCast(col.bounds.center, col.bounds.size, 0, Vector2.down, 0.1f, groundLayer);

This creates a box to check weather you are grounded instead of a line, fixing the issue stopping it from jumping near edges and uses the LayerMask 'groundLayer' to make sure it only counts collisions with the ground and not any backgrounds (make sure to include all objects you want to be able to jump on in a layer include).

Feel free to check documentation for anything in the code I've written which you don't understand or just leave me a reply and I'll try to respond.

of
Автор

8:45 if you are using tileset, remember to set A* to Collider Type Point and set the right layer of your tileset to match in the Obstacle Layer Mask Within A*

Oliver_Mann
Автор

This worked better for me than the brackeys tutorial.. I already don't like importing other assets, but gave this one a pass since there's no default 2D navMesh option. Honestly I want to apologize started this tutorial first, then noticed brackeys had one, I immediately stopped this video to go to his (because he's trusted), however his configuration didn't mesh with my character well. I then came back here. I will admit though it took me much longer than 24:40 to get this to work, but it finally did, Thank you for making this.

DrFross
Автор

thanks a lot for this. i first tried doing this from brackeys video but i just couldn't get my AI to jump. and i wasn't satisfied with my enemy flying around

yxshv
Автор

For anyone wondering, baking the grid is in the scan option at the bottom of the Component (named AstarPath) or can be called via script with AstarPath.Scan() in case you need to rebake it on runtime

victormeas
Автор

how do you get the grid to recognise the ground objects if you're not using a tilemap?

pippasoul
Автор

The "refresh button" you are referring to at 8:50 is the 'Scan' button, right above 'Add Component' on the bottom.

faanross
Автор

Awesome video, helps a lot with simple enemies and also with flying enemies (like Bats 🦇)

TheScorpionAly
Автор

keep getting the error enemy updatepath couldnt be called?

Chuckzplace
Автор

For smooth moving you can use:

private Vector2 currentVelocity;

rb.velocity = Vector2.SmoothDamp(rb.velocity, force, ref currentVelocity, 0.5f);

against:

rb.AddForce(force);

shellbox-studio
Автор

hey i basicaly copied the script word for word but my private Path path always seem to be null and honestly i have no idea when its supposed to get set as something

tobiasbn
Автор

hi, do u maybe know how my enemy could walk up a 45° path? its get stucken in the corner on the bottom and the Node size is at 0.05

Thrymbor
Автор

Im gettin a weird error saying "ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" Anyone have any idea why this is happening?

Emerald_Dev
Автор

direction.y gave some weird outputs for me and resulted in lots of bugs involving jumping. If anyone is experiencing this too, then I made a hacky fix for it.

Instead of direction.y > jumpNodeRequirement

I wrote target.position.y - 1f > rb.transform.position.y && targetrb.velocity.y == 0 && path.path.Count < 20

Explanation:
This code checks if the target position is above the position of the enemy (adding 1f to avoid excessive jumping in case it's a bit lower down on the y axis than the target). Then it checks if the velocity of the target is 0 (I do this to avoid it jumping while the target jumps, since that looks dumb). Then I check how many nodes away the enemy is, if it's bellow 20 it can jump (I do this to avoid it looking like a bunny, this could be used to make a banger of a bunny NPC).

dubble.
Автор

The reason your enemy is not moving horizontally well is likely due to its rigid body having drag and friction in it. Add a custom physics material to it to fix this. Playing around with the speed is not a good way.

attenurmi
Автор

When the enemy is on a platform on top of the player, it doesn't ever go down the platform. How could I fix this? Instead it just goes from left to right not following the path

rodrigocortez
Автор

Hi how can you make the enemy has 2 targets. so I can make like a patrol behaviour thing. thanks

oloy
Автор

OMG thanks for this fully functional AI :)

mrdanceroriginal
Автор

For the people having problems with a NullReferenceException is probably because you are not using the "seeker" script, you need to add component and type seeker and add the script, not sure if its mentioned in the video but here is the fix.

josue
welcome to shbcf.ru