How to get the position where 2 parts touched?

Hello
I have a big anchored part and a smaller moving part which hits the anchored part.
The smaller part does not move in a straight direction before the hit.
Is there some way to get the coordinates where the touch happened?
When the Touched event fires the small part is not too close anymore to the point of the hit.
(the Roblox engine should know where the hit happened, because it physically simulates the bounce at these coordinates)

Thanks

The easiest way to do this that I can think of is to use raycasting to the bigPart from the smallPart when they are touching.

Here is the code, I hope it works

local bigPart = workspace.bigPart
local smallPart = workspace.smallPart

local function onTouched(otherPart)
      local rayOrigin = otherPart.Position -- Cast from smallPart
      local rayDirection = bigPart.Position -- Cast to bigPart
      local raycastResult = workspace:Raycast(rayOrigin, rayDirection)  -- Begin raycasting

      if raycastResult ~= nil then         -- Once the ray hit the bigPart, return the position where the ray hits.
            print(raycastResult.Position)
      end
end

bigPart.Touched:Connect(onTouched)

use this instead

local bigPart = workspace.bigPart
local smallPart = workspace.smallPart

local function onTouched(otherPart)
      local rayCastParams = RaycastParams.new()
      local rayOrigin = otherPart.Position
      local rayDirection = (bigPart.Position - otherPart.Position).Unit * 9e9
      local raycastResult = workspace:Raycast(rayCastParams, rayOrigin, rayDirection)

      if raycastResult ~= nil then
            print(raycastResult.Position)
      end
end

bigPart.Touched:Connect(onTouched)

shooting a ray from the small part to the big part wont give you the exact hit position

image


but you can use Shapecast

this should give you the accurate hit position

6 Likes

It is even worse, because at the time when OnTouched event fires the small part is below the big part (falling due to gravity) so the raycast may not hit at all the correct side of the big part.

this is because of network ownership and the physics step not running in sync with luau

the computer that currently has ownership should be detecting the touch

you can set the networkowner to nil so that the server keeps ownership and then the touch event should fire at the correct time on server scripts

another problem is that the physics engine does not run in sync with luau code so it also possible that the physics step can run multiple times after the touch before the luau event even gets fired

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.