This is a difficult raycasting problem

image
A player launches a ‘toilet plunger’ to a wall surface.

The toilet plunger uses raycasting.
image

The toilet plunger is meant to land like that ^^

Use:
the raycast.Normal. The previous position of the plunger and the new position to get a ‘direction’ along the surface where it should face and use the raycast.Position.

All these should build the CFrame for the plunger

1 Like

Well, you want to position to the intersection, looking in the direction of the normal, and then moving half the length of the plunger in that direction,

  1. Get rayresult.Position, and get the normal direction. The normal direction is made into a unit, I assume, so just add it to the position to get a vector from the position to the unit distance in the direction of the nomal
  2. CFrame.lookat(rayersult.Position,rayresult.Position+normal)
  3. Plunger is orientated frontward? If so, multiply CFrame by * CFrame.new(0,0,-size.Z/2)
CFrame.lookat(rayersult.Position,rayresult.Position+normal) * CFrame.new(0,0,-size.Z/2)
1 Like

Yes but where is the plunger facing towards relative to the front? Assume that the ‘stick’ is the upvector. Which is the lookvector?

Right click, and find this button
77a8be2fc9cf14bbf040e40f4596d7cc

Okay. Now how do I get a lookvector from just the UpVector or in this case the raycast.Normal?

Not sure why you’d need that but look into using Vector3:Cross().

because the lookvector will determine where the…
Okay let’s say that the plunger has a yellow stripe in the front.
The look vector will make sure the yellow stripe points according to the velocity of where it is being thrown.

if you place your plunger using this math:

plunger.CFrame = CFrame.new(hitPosition, hitPosition + raycast.Normal)

Then your plunger will be oriented with its lookVector (one of the stick’s 4 sides, perpendicular to its upVector) parallel to the raycast’s Normal.

You can then rotate the plunger along the X axis by 90 degrees so that the upVector aligns itself with the raycast’s Normal, which should give you what you want:

plunger.CFrame = CFrame.new(hitPosition, hitPosition + raycast.Normal) * CFrame.Angles(math.pi / 2, 0, 0)

For a part that’s aligned to the world’s axes, the lookVector is pointed towards NEGATIVE Z (rightVector is pointed to +X, and upVector is aligned with +Y). Selecting a specific part and trying to translate it along its local coordinates will show you the corresponding colored axes, with red being X (parallel to its rightVector), green being Y (parallel to its upVector), and blue being Z (parallel to its lookVector):
image