So, I made a code on my project so that my bullets will go out of a Marker2D attached on a Harp Sprite attached on a Marker2D on the middle of the player
The code (NOTE: “bolinha” is the bullet", “Muzzle” is where the bullets get out, “Harpa” is the Harp sprite and “Harpa Rotation” is the Marker2D on the middle of the player):
func shoot():
var b = bolinha.instantiate()
b.start($“Harpa Rotation/Harpa/Muzzle”.global_position, rotation)
get_tree().root.add_child(b)
Additionaly, the Marker2D on the middle of the player is programed to follow the mouse, wich makes the Harp follow the mouse on a circular direction
extends Marker2D
The code:
var mousepositoion
func _process(delta):
mousepositoion = get_local_mouse_position()
rotation+= mousepositoion.angle() * 1
Both work just fine, but my problem is that when I shoot, the bullet’s angle will always be lef or right, but I want the bullet’s angle to be the same the Harp is currently in
What code do you use to set the rotation of the bullet?
The bullet doesent rotate, its the point of origin that rotates
Anyway, her’s my bullet’s code:
extends PhysicsBody2D
class_name projectile
var speed = 300
var velocity = 50
func start(_position, _direction):
rotation = _direction
position = _position
velocity = Vector2(speed, 0).rotated(rotation)
func _process(delta):
pass
func _physics_process(delta):
var collision = move_and_collide(velocity * delta)
if collision:
velocity = velocity.bounce(collision.get_normal())
if collision.get_collider().has_method("hit"):
collision.get_collider().hit()
func _on_visible_on_screen_enabler_2d_screen_exited():
queue_free()
func _on_timer_timeout():
global.active_bullets = 0
queue_free()
func _on_visible_on_screen_notifier_2d_screen_exited():
global.active_bullets -= 1
queue_free()
func _on_area_2d_body_entered(body):
if body.is_in_group("targets"):
global.is_dead += 1
$Destroy.play()
You put the bullet on $“Harpa Rotation/Harpa/Muzzle”.global_position
but set its rotation to the current object rotation? Shouldn’t it be something like $“Harpa Rotation/Harpa/Muzzle”.rotation
??
the code crashes when I do that