1305 shaares
176 private links
176 private links
using Godot;
public class GameController : Node
{
// PackedScene containing the bullet node
private PackedScene bulletScene;
// PackedScene containing the decal node
private PackedScene decalScene;
public override void _Ready()
{
// Load the packed scenes
bulletScene = ResourceLoader.Load<PackedScene>("res://Bullet.tscn");
decalScene = ResourceLoader.Load<PackedScene>("res://Decal.tscn");
}
public void ShootBullet(Vector3 startPosition, float speed)
{
// Instantiate the bullet node from the packed scene
var bullet = (RigidBody3D)bulletScene.Instance();
AddChild(bullet);
// Set the bullet's initial position and velocity
bullet.Translation = startPosition;
bullet.LinearVelocity = Vector3.Forward * speed;
// Connect the bullet's collision signal to a method that will handle collisions
bullet.Connect("body_entered", this, nameof(OnBulletCollision));
}
private void OnBulletCollision(Node body)
{
// Destroy the bullet node
var bullet = (RigidBody3D)body;
bullet.QueueFree();
// Instantiate the decal node from the packed scene
var decal = (Spatial)decalScene.Instance();
AddChild(decal);
// Set the decal's position to the point of impact
decal.Translation = bullet.GlobalTransform.origin;
// Set the decal's rotation to match the surface normal of the collision
if (bullet.GetCollisionNormal() != Vector3.Zero)
{
decal.Rotation = bullet.GetCollisionNormal().Normalized().Orthogonalize();
}
}
}