My question is:
Can you redirect a ray once it’s created
local ray = Ray.new(blahblah.Position, blahblah.Cframe.LookVector)
--and then change the rotation of blahblah and say again something like
ray.Direction = blahblah.Cframe.LookVector
please tell me if this is possible which i suspect it’s not,
Not through API, you’d just recall the raycast by getting the end position result then get the vector difference of the end result and origin (
blahblah.Position - blahblah.Position, order matters so if this isn’t correct, it’ll just be the reverse)
That difference between the origin and end result of the raycast is a vector that you can use as the direction to go back from, or you could probably get a negative version of your original direction (
I also recommend using the new raycast API that replaces the need for
Ray.new, you can still however use
Ray.new for the origin and direction if needed (
yourRay.Origin or yourRay.Direction).
so you basically say that I should use workspace:Raycast() ?
Yep, you don’t even have to include a
RayParams but you can use the
RayParams API if you need, it is a little neater in my opinion than filling out arguments for
:FindPartOnRayWithWhitelist() even if a little lengthier.
but my question is:
once the ray iis already created can you modify it’s direction?
If you didn’t raycast yet, you can modify the direction to make a new ray. If you mean to change it during raycast or after raycast, the Ray object, yes, but then you’d still have the same origin. You might as well create a new ray at that point. There is no reason to modify the direction if you’re hoping to reflect the ray back.
Here’s code I created on the fly that might represent what you want:
local function raycastThenGetResultBack(origin, direction)
local firstRaycast = workspace:Raycast(origin, direction)
if not firstRaycast then
position = origin + direction -- no instance was found, thus complete the result for ourselves
position = firstRaycast.Position
-- instead of -direction, you could also do (origin - position) or (position - origin) whichever is correct
local returnRaycast = workspace:Raycast(position, -direction)
i am currently making a sword and when it hits it creates a ray that checks if it crosses with another sword but the issue is server performance. with so many rays that are created with the sword moving around i think it’s easier for a ray to change direction than creating 7mil rays that do the same thing. I’m just saying
I believe the less rays you use the higher the performance, direction changing just means making a new ray regardless anyways. The lengths of the ray also matter on performance IIRC.
and sad in the same time lol
The answer to this is no. Rays are immutable (fancy language that just means that it doesn’t ever change. A mutable object can change). Roblox data types (like Ray or Vector3) are immutable. So if you want a value inside of them to change, you need to create a new one.
This is OK and won’t actually cause any performance problems. A Ray object itself only contains some vector information, but doesn’t perform a raycast operation. The actual raycasting itself is much more costly.