Godot Version
4.2
Question
I have a laser RayCast3D scene and I want it to bounce of off certain objects in my game. The laser itself is a RayCast3D as I mentioned and it has a mesh that follows the RayCast3D to where it collides. Is bouncing a RayCast3D even possible? If it is I kindly ask you to help me with this. If not how can I achieve a similar effect?
I’m not sure if there’s a way to make it reflect directly, but you could always instance another raycast where the first one collides and set its rotation based on the normal of the collider.
For this you should probably use the physics server directly as the built-in system does not support bouncing, you’d have to use intersect_ray
and similar to accomplish this, using the normal
return value together with the direction of the ray to compute some reflected direction, and probably bounce
or reflect
depending on your needs
Wow that is some complicated stuff there. I can be considered a beginner, so please take that also into account :).
I had this code for the reflection of the laser. I was wanting to make the lasers bounce off of only mirrors.
if(collided_object is Node node)
{
if(node.IsInGroup("Mirrors") && laserReflected == false)
{
laserReflected = true;
var scene = GD.Load<PackedScene>("res://Laser.tscn");
RayCast3D instance = scene.Instantiate<RayCast3D>();
AddChild(instance);
foreach(CollisionObject3D mirror in mirrors)
{
instance.AddException(mirror);
}
instance.Position = cast_point;
var reflection = (cast_point - ToLocal(instance.Position)).Reflect(cast_point_normal.Normalized());
var angle = cast_point.AngleTo(reflection);
Vector3 newRotation = instance.Rotation;
newRotation.Z = Rotation.Z + angle;
instance.Rotation = newRotation;
}
}
Let alone the rotation of the new laser, the position also does not work properly. the middle of the laser goes to the cast_point for some reason.
As I said, don’t use the node, do it directly with the servers, but this isn’t simple stuff so you will have to learn quite a bit of the engine to use it