Arrow as rigidbody2D or area2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BrunoFreezee

i wanna make an arrow movement, the arrow need to be really fast and stop immediately on the wall (like it has the arrowhead pierce in the wall)

Could you provide more details? What do you mean by “arrow movement”? Are you using a RigidBody2D or an Area2D for it?

Ertain | 2020-06-24 17:05

Hi,
I hope it helps, I believe is an Area2D, have a look at this tutorial, it may you have seen it or not.
https://www.youtube.com/watch?v=3gHdnF1FZ1w

NOTE: I am not sure if is allow to post links like the above, I have nothing to do with the author, please let me know if I ned to remove it.

Jorge | 2020-06-24 17:15

I wanna know what node i should use for this, I already know how to instance the arrow when I want, i just need to know the best way to move it forward and make it stops on the wall

BrunoFreezee | 2020-06-24 17:17

it is fine to share links, however, I had already seen, it does not help much

BrunoFreezee | 2020-06-24 17:19

For stuff that needs to move, yet also needs to interact with the surrounding collisions, I use a KinematicBody2D.

Ertain | 2020-06-24 18:04

:bust_in_silhouette: Reply From: Czselu349

Hello!
Answer to question:
Both options are good, but in your case I’d use Area2D. If you’re using an arrow, than you’ll probably have to update the global_position of it, because otherwise, if you move the parent node, it moves too. You’ll need to make your own collisions, but I think, that haveing a RigidBody2D would be unnecessary, because you won’t really use physics for it’s movement.

My advice:
I think you should use a State Machine, because you can set states, so when you shoot the arrow, it’s in a moving state, if it hits something it’s in a stopped state.
You can obviously make the script (and the state machine) really simple, but I recon’ spending some time with it because you can reuse it, and it makes you code be nicer.
I only know this tutorial, but you can totally apply it to kinda everything:

this is a part of the tutorial:

but if you don’t want to have complexity to your arrow, then I made a simple state machine inside this little code section:

extends Area2D

# Varaiables for moveing
var move_direction = Vector2(0,0)
export(int) var speed = 10
var states = ["fly", "stop"]
var current_state
var pos
# Damage
export(int) var dmg = 10

# When I spawn the bullets I give them a direction to go,
# In the parent node. Obviously you can change tha direction around if you want to.

func _physics_process(delta):
	if move_direction != Vector2(0,0):
	current_state = states.fly
match current_state:
	states.fly:
		global_position += move_direction * speed
		global_rotation = move_direction.angle()
		pos = global_position
	states.stop:
		global_position = pos
func _on_Area2D_area_entered(area):
#	if area.is_in_group("PlayerHitbox"):
#		area.get_parent().take_damage(dmg)
#		destroy_bullet()
if area.is_in_group("wall"):
	current_state = states.stop

It worked just how I wanted, of course I’m gonna make the improvements I need.
Thank you very much, great answer, easy code after all :slight_smile: Thank you

BrunoFreezee | 2020-06-24 21:10

if the speed is too high, the arrow goes through the wall, some way to solve this?

BrunoFreezee | 2020-06-24 21:36

You’re welcome! :slight_smile: I’m just trying to be a part of the community.

About the collisions not working, if the speed is too high: My only guess is that it goes so fast, that the game doesn’t register that, the collision has been made. I didn’t encounter such problem before, so I can’t really think of anything else.

Czselu349 | 2020-06-25 11:26