I’m trying to make blood, it doesnt use raycasting, it uses velocity, so I need to raycast in the direction the velocity is going to check if its hitting something but I cant figure out how, how would I do this?
Example:
I’m trying to make blood, it doesnt use raycasting, it uses velocity, so I need to raycast in the direction the velocity is going to check if its hitting something but I cant figure out how, how would I do this?
Example:
There’s a couple of ways to do this, but I think in any case you’ll probably end up checking after the blood particle moves. One simple way you could do this is to compare an old position with a new position, since you’re checking after the particle moves this is pretty easy to do.
local RunService = game:GetService('RunService');
local ray_params = RaycastParams.new();
local function check_collision(last_position: Vector3, position: Vector3)
local ray_result = workspace:Raycast(last_position, position - last_position, ray_params);
return ray_result;
end
local blood_part = workspace.BloodPart; -- Update this to whatever you want the blood part to be.
local last_position = blood_part.Position;
RunService.Stepped:Connect(function()
local collision_result = check_collision(last_position, blood_part.Position);
if (collision_result) then
print('Collision detected!');
end
end);
No need to use this exact example, since you may not need to check this 60 times per second, or whatever your current stepped rate is in the game. But the check collision function is a pretty basic way to do this.
Another option is to use last_position
, last_velocity
and delta_time
where delta_time
is the time since the last simulation. That could be written as such:
local function check_collision(delta_time: number, last_position: Vector3, last_velocity: Vector3)
local ray_result = workspace:Raycast(last_position, last_velocity * delta_time, ray_params);
return ray_result;
end
You could also do future predictions, but since there’s no way to actually predict how long a “delta time” is then it wouldn’t be quite as accurate. You could use a fixed simulation rate for something like that. Could look like this:
local RunService = game:GetService('RunService');
local ray_params = RaycastParams.new();
local physics_simulation_rate = 1 / 60;
local function check_collision(position: Vector3, velocity: Vector3)
local ray_result = workspace:Raycast(position, velocity * physics_simulation_rate, ray_params);
return ray_result;
end
local blood_part = workspace.BloodPart; -- Update this to whatever you want the blood part to be.
RunService.Stepped:Connect(function()
local collision_result = check_collision(blood_part.Position, blood_part.Velocity);
if (collision_result) then
print('Collision detected!');
end
end);
Pretty sure any of the three will work fine for this use case.
If you already know how to raycast, you need to use AssemblyLinearVelocity
to figure out it’s velocity. If you don’t know how to raycast, see here:
I would definitely check before the blood particle moves, because if OP wants to make some kind of splat effect using a surface gui and a part, There is a chance one the blood could already moved behind the part it’s colliding with. Therefore you wouldn’t be able to see the splat.
As mentioned in my explanation there are pros and cons to each. Physics simulation rate is variable and entirely unpredictable. It’ll try to run at a fixed rate but that’s entirely dependent upon available resources. Either way, he’ll have issues like this one. I just really question whether that’s going to be a realistic concern. If someone like that occurs, it would either be moving extremely fast or it’s very small. In either case it would likely be an imperceptible collision to the human eye.
I agree with you. I think it’s very frivolous, but if it is, there shouldn’t be an entire 100 line script for simulating it. Of course we still want it to be realistic, but not too realistic. This isn’t RTX. I think a simple velocity script should do. Even then, you could rely on the physics engine. Unanchor the blood, then set it’s velocity, maybe add a few collisiongroups here and there, and when it touches something… SPLAT! This would be VERY simple and not taxxing.
Another thing, You don’t have to worry about running it at a fixed rate, because using the deltatime argument that RunService.RenderStepped
, or I think it’s RunService.Heartbeat
provides, no matter the “resources” of the computer, you can accurately reposition the blood speck by determining the amount of time passed.
Another thing, You don’t have to worry about running it at a fixed rate, because using the deltatime argument that
RunService.RenderStepped
, or I think it’sRunService.Heartbeat
provides, no matter the “resources” of the computer, you can accurately reposition the blood speck by determining the amount of time passed.
This is only the case if you check for collisions after the part moves. Delta time is in the past.
I agree with you. I think it’s very frivolous, but if it is, there shouldn’t be an entire 100 line script for simulating it. Of course we still want it to be realistic, but not too realistic. This isn’t RTX. I think a simple velocity script should do. Even then, you could rely on the physics engine. Unanchor the blood, then set it’s velocity, maybe add a few collisiongroups here and there, and when it touches something… SPLAT! This would be VERY simple and not taxxing.
I was just answering OP’s question the way he asked it. I assume he has a reason to do it the way he’s doing it.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.