How to inverse a Vector3 direction

I’ve came up with a system to make a wallbang with the gun I’m scripting. Pretend the red line in each picture is the RayCast, and the white square in each picture is the origin point of the ray.

Once the first ray hits the wall, I can create another ray on the other side of the wall, with the same direction as the last ray, but inversed. This will allow me to detect where the bullet leaves the wall.

Is there a way for me to inverse the direction of the first ray?

1 Like

To invert a Vector3, use the negate operator.

local v = Vector3.new(1,1,1)
print(-v) --> -1, -1, -1

Is that what you want?

3 Likes

You would change the rays direction to a negative vesion of itself (RayDirection * -1) and obviously, move the Rays Origin, you could (maybe) do this by before changing the RayDirection to a negative, you get the position by the RayDirection * 5 + Ray.Position --Ray.Position being the hit location of the ray.

Would that give the direction of the 2nd ray? I was imagining there being a built in function, similar to CFrame:Inverse()

Also, would this error if one of the numbers are 0, because it’s trying to make a negative 0?

Negative zero is just zero, it won’t error. I still don’t really know what you’re trying to do.

Good idea, I think I could do something like this for determining the 2nd ray’s origin:

Ray2Origin.Position = Ray1Origin.Position + Ray1Origin.CFrame.lookVector * 5 -- places the 2nd ray's origin 5 studs in front of Ray1Origin
Ray2Origin.Orientation = -Ray1Origin.Orientation -- inverses the orientation of the 2nd ray origin

I think what you gave will work