I'm making a cover system and so far, I have it so that when the player presses 'C', it casts a ray from the character's lower torso's look vector about 5 studs out. If the character detects cover, then it will play a cover animation. Everything works perfectly so far. The next course of action is sticking the character to the cover. I was thinking maybe detecting the normal of the hitPart and somehow using that? I honestly don't know where to start with this, let me know what you think. Any help is appreciated.
you can use CylindricalConstraints to only allow an object to move on one axis https://developer.roblox.com/en-us/api-reference/class/CylindricalConstraint
as for actually figuring out how the Attachment0 should be rotated to get said axis, you can use the cross product of the surface normal and
Vector3.new(0, 1, 0)
cross product will give you a vector which is perpendicular to both of the vectors you gave it; there are two possible vectors which can be perpendicular, so which one it gives you is dependent on the "right hand rule"
local function EnableCoverConstraint(surfacePos, surfaceNormal) local upVector = Vector3.new(0, 1, 0) local rightVector = upVector:Cross(surfaceNormal) local backVector = rightVector:Cross(upVector) -- all three "vectors" of a cframe have to be perpendicular to each other, so we need to get a new "back vector" -- CFrame.fromMatrix uses a back vector instead of a lookvector so. Yeah -- the rest of this code assumes that you just have a CylindricalConstraint parented to the HumanoidRootPart at all times, making a new one every time is kind of nasty when you can just disable it when it's not in use -- these are just example names btw, you can name the Stuff whatever you like HumanoidRootPart.CoverAttachment0.WorldCFrame = CFrame.fromMatrix(surfacePos + surfaceNormal, -- adding the surface normal pushes it away from the wall a bit, so your character isn't literally right next to it rightVector, upVector, backVector) HumanoidRootPart.CoverAttachment1.WorldPosition = surfacePos + surfaceNormal HumanoidRootPart.CoverConstraint.Enabled = true end
i haven't used cylindrical constraints before like, ever but i think you'll have to set the InclinationAngle to either 90 or -90