Godot Version
4.2.2
Question
I want bullets to emit in the direction an object is travelling. Currently, they leave to the object’s right rear—ugh! Am I missing something obvious?
- %ObjectParent (Node2D)
– Object (RigidBody2D)
---- Container (Node2D)
———Emitter (Node2D)
# OBJECT CODE
# This is called from the Emitter
func return_velocity():
var objVelocity = linear_velocity
return objVelocity
# EMITTER CODE
extends Node2D
@onready var projectile = load("res://Balls/Projectile.tscn")
func _physics_process(_delta):
objVelocity = $"../..".return_velocity()
self.global_rotation = objVelocity.angle()
func fire_projectile():
var instance = projectile.instantiate()
instance.dimacr = rotation
instance.spawnPos = global_position
instance.spawnRot = rotation
%ObjectParent.add_child.call_deferred(instance)
await get_tree().create_timer(1.0).timeout
fire_projectile()
# PROJECTILE CODE
extends CharacterBody2D
var SPEED = 300
var dir : float
var spawnPos : Vector2
var spawnRot : float
func _ready():
global_position = spawnPos
global_rotation = spawnRot
velocity = Vector2(0, SPEED).rotated(dir)
func _physics_process(delta):
var collision = move_and_collide(velocity * delta)
if collision:
velocity = velocity.bounce( collision.get_normal() )