Projectile and RayCast2D in Godot 4
Вставка
- Опубліковано 7 жов 2024
- Projectile / Bullet mechanics in Godot 4 using Ray Cast where it shoots only when it detects the player.
Code -
Player - pastebin.com/X...
Bullet / Projectile - pastebin.com/y...
Turret / Gun - pastebin.com/5...
Music Used -
Hoogway - Found You
Provided by Lofi Records
Watch: • Hoogway - Found You
Download/Stream: fanlink.to/Equ...
bruh took me 10min to find whatever why the sprite was not looking at player, then i come to this video and find out that i just have to put "ToLocal". Now i ask why is it a thing, it does work now. Thanks
Where do you put ToLocal?
@@nolanbudzien2466ToLocal converts global to local. its a method, you use it first to convert a global position to local, this might solve issues if you have any. 3:03 Most likely to rays.
youtube didnt notifiy me of that comment
Thanks for more amazing content
thanks man from brazil
Any possible reason why I would be getting this error?:
- Invalid set index 'direction' (on base: 'Area2D (EnemyBullet.gd)') with value of type 'Vector2'.
When I edit out this line of code, the combined scene is able to play:
# ENEMY_BULLET.direction = (RAY_CAST.target_position).normalized()
is there a way to add range for ray? or do we have to create a collision to detect player for making a range?
I add a comment in paste bin url with the modification for limit the range to 250px
Its easier thank i thought thanks
everything works except this:
Parse Error: Cannot call non-static function "instantiate()" on the class "PackedScene" directly. Make an instance instead.
it says this right at the end when i try to instantiate the bullet
(btw i am using a character body 2D for the turret)
voce conseguiu resolver isso ?
@@Eudesenho-og3jl make a variable first for instantiate the bullet
I keep getting this syntax: Invalid get index 'position' (on base: "null instance"). Do you know what could be happening?
did u drag and drop the bullet scene into the export variable? if the export variable is empty, then it throws this error
Thankyou, yes that helped!@@16bitdev
Nice work. Stills working in 4.2.2 :)
thank you so much for the video🧡. can you make a video or explain me how I can add a range in which the turret will see the player? sorry I just started to learn gamedev
add Area2D to that turret, inside it add CollisionShape2D, give it a circular shape with desired radius, if a player enters that area turn on the turret (using body entered signal) and start shooting, if the player leaves then turn it off (using body exited signal)
did all that, but how I turn if off? in the shoot func or where? @@16bitdev
@@16bitdevExcuse me, how can I activate the turret when entering the 2D area?
When I start my game I get this error:
Invalid get index 'position' (on base: 'null instance').
me too
@@qvell got the solution?
i removed player and dragged n dropped my player in as a string instead but i still cant get the turret to fire lol
Why isn't working on 3d
helped me
In my game im doing a ray cast from the front of an enemy, if the player hits that ray cast the enemy will shoot. The issue I'm having is anything and everything the Raycast collides with comes back as true.
i ended up figuring it out, it was a combination of bad checks that were not being identified correctly@ddwaveking
good job
This code for the bullet desnt work. When I implement it, the bullets are coming out at a 90 degree angle to the right of the ray cast. I followed the code as 1:1 as possible, yet I have no idea what is causing this ( on v4.2.1)
update direction of the bullets to player while spawning them, by default its Vector2.RIGHT
Cheers man
Why is the raycast moving using player movements instead of targeting player?
It is targeting player, that's why its pointing towards player.
@@16bitdev ok
Invalid set index 'target_position' (on base: 'RayCast2D') with value of type 'Vector2'.
if you are using godot 3.5, then its "cast_to", in godot 4 they renamed it to "target_position", change the property name from "target_position" to "cast_to"
@@16bitdev thanks :D
Thanx
How set raycast collision mask 1 and 2 on code
RayCast2D(node).set_collision_mask_value( Layer_number, true)
How do you get the raycast to show up as a red arrow?
if raycast detects collision it automatically turns red, make sure the collision body and the raycast are masking properly.
turn on Debug -> Visible Collision Shapes on the top bar in Godot 👍
how do you make the bullet kill the player?
for player, give a variable 'health', and a function that decreases health (func take_damage()) , now the bullets are Area2D, they have a signal called body_entered(body), so when a player comes in contact with the bullet, body.take_damage() gets called, when health goes
thank you!@@16bitdev
Not sure why but my bullets aren't going in the scene
first instantiate the bullet scene,
then set its position,
then add it to the scene by typing,
get_tree().current_scene.add_child(bullet)
Thank Man my script od working 💲