In this article, I will guide you to create raycast2D so that the enemy can move on the terrain as shown below without falling down. And let’s get started
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
RAYCAST2D EASY
Here Raycast 2D is 1 node: Query object intersecting ray means Raycast 2D is 1 ray and this ray is used to query object when intersecting ray
ENEMY
Next is the enemy you enter the enemy’s sence
Find yourself raycast2D
And be like this
Here you do not use the mouse but use the right arrow key to move it to the edge collsionshape
And almost as open as me
On the side of the nature, pay attention to yourself Enable and Cast to
live Enabled you click for yourself and raycast will turn blue
LIVE cast to part y you cast it close under the enemy
Like the picture, this enemy is too small so I’ll adjust the scale raycast wall 0.5, 0.5
And after adjusting the scale, you also have to adjust the other part
Next we will code it
And Coding for me
Below if is_on_wall
You add yourself as
if $Raycast2D.is_collding == false:
vitri = vitri * -1
Here $Raycat2D is called raycast2d || is_collecting is the command: Returns whether any object is intersecting the vector ray which here we attach = false means it will not intersect at all.
And vitri = vitri * -1
here means the enemy will move in reverse when on the terrain that I mentioned at the beginning of the article
And you notice when it has moved back but this raycast2D it doesn’t move back anymore so we need to adjust it.
You add $RayCast2D.position.x *= -1
into the if is_on_wall and raycast collecting Please
This is the help command Raycast 2D can change direction and you can apply to other nodes like collsionshape etc
It is done
And remember to delete *delta let’s go
If the code does not run, the code is as below