Applying foward momentum to a vector

I’d like to fire the projectile in a straight line from the angle of the $neck/barrel.global_transform but in it’s current implementation it fires at strange angles from barrel. Please excuse any glaring oversight or mistake, I’m still a novice.

Firing Projectile

func fire_loaded_round():
	var r = pl_round.instantiate()
	owner.add_child(r)
	r.speed = $neck/barrel.global_position
	r.transform = $neck/barrel.global_transform
	print(r.speed)

Projectile

extends CharacterBody2D

var speed = Vector2(300,0)

func _physics_process(delta):
	var collision_info = move_and_collide(speed * delta)
	if collision_info:
		speed = speed.bounce(collision_info.get_normal())

What is the bullet a child of? What is the owner node? If this parent can rotate or move then the projectile will rotate and move with it, causing plenty of problems.

Your speed isn’t based on the projectile’s forward vector, it’s based on a the parent’s right vector. In 2D transform.x will be the node’s right vector, try this out.

extends CharacterBody2D
var speed: float = 300

func _physics_process(delta):
	var collision_info := move_and_collide(transform.x * speed * delta)
1 Like

The owner is the player and can move/rotate.
Thank you, your fix works but it breaks the bullets ricocheting properties, I’m assuming this is the problems you said it might cause.

The collision needs to change the object’s rotation to correctly bounce; maybe this will work?

extends CharacterBody2D
var speed: float = 300

func _physics_process(delta):
	var collision_info := move_and_collide(transform.x * speed * delta)
	if collision_info:
		rotation = transform.x.bounce(collision_info.get_normal()).angle()
1 Like

I thought that too but I get the error “Cannot find property “get_angle” on base “Vector2”.”

Sorry, just .angle()

1 Like

Thank you very much!!