I’m a bit stuck as to where to start this one. Basically, I want to align a cylinder to a wall whilst retaining the cylinders original X rotation.
I will outline both the effect I want, and how I want the maths to work, but if you have a better suggestion for how I should do the maths, please let me know. As far as my vector maths knowledge goes, I’m pretty strong with trig and dot products, but I’ve never done cross products or anything more advance.
So this is my TNT, it could be coming in at any angle. Its not using Roblox physics, its using SUVAT + ray casting. I want it to hit the roof of the building, and stick it, like a sticky bomb (not like actual TNT lol)
So it should end up like this. What I want to do is as soon as my raycasts hit the roof, the script stops the SUVAT stuff and instead calculates the position and rotation the part needs to be in. It will then quickly tween into place, as naturally as I can make it look
Any help (inc, just linking a relevant tutorial) would be much appreciated!
Most of the calculations can be done with the CFrame constructor.
You’ll need some data from the raycast, namely the world position of the impact and the surface normal of the wall that the TNT hit.
local normal = (surface normal vector of the wall, you can get this from FindPartOnRay)
local contact = (position of where the ray hit the wall)
-- Offset the contact position to account for the size of the TNT stick.
local offset = contact + normal * (one half of the width of the TNT cylinder)
-- Then calculate the CFrame of the stick when it is laying on the surface.
-- This is assuming the top and bottom surfaces of the primarypart represent the ends of the stick.
local resultCFrame = CFrame.new(offset, offset - normal)
If you have done dot product, then I will assume you know what projections are.
Basically, you can project the top vector of your TNT onto the plane (surface) that the TNT hits. You would need to retrieve this from the surface’s CFrame data. That projection would then be your new look vector for your TNT. Note that rotating to this vector keeps the X orientation intact.