Hi there,
I'm having trouble getting a projectile to travel the way my player is facing. Currently, whenever I fire one, it travels in a 'due north' direction (0, 0, -1). In addition, the projectiles always face the same way regardless of how my player is facing.
Here are the relevant parts of my code. (The rest of the project code in my src folder can be found here.) In a shoot() function within main.cpp, I load a new instance of my projectile scene, then set the transform of the projectile equal to the player's current transform:
void Main::shoot() {
godot::Node* new_projectile = projectile_scene->instance();
godot::Transform player_transform = _player3d->get_transform();
new_projectile->set("transform", player_transform);
add_child(new_projectile);
}
(Note: both my projectile and player objects are of the KinematicBody type.)
Then, within the physics_process function of the projectile, I retrieve the current transform, multiply it by a projectile_speed value to calculate its velocity, then use move_and_slide to make the projectile move:
void Projectile::_physics_process(float delta) {
godot::Transform projectile_transform = get_transform();
godot::Vector3 projectile_velocity = projectile_transform.basis.z * projectile_speed * -1;
projectile_velocity.y -= fall_acceleration * delta;
projectile_velocity = move_and_slide(projectile_velocity, godot::Vector3(0, 1, 0));
}
The projectiles do begin at the location of the player, but they aren't taking the player's rotation into account. How can I fix this? Based on this GDScript shown within the 'Obtaining Information' section of the Using 3D Transforms, I thought that setting the projectile's transformation equal to the player's transformation would make the bullet travel in the direction the player was facing.
Thank you in advance for your help!