Making an object fit the slope of a triangle terrain mountain

Hi there,

I’m trying to make a part, or an object having 1 surface fitting exactly the terrain. I’ve tried with orientation of the mouse.Target,

However I knew it wouldn’t work correctly mostly because every triangle is oriented to fit the other triangles, so none of the triangles have a logic orientation.

Is there a way to just make two surfaces fit eachothers?

image
image

The way I place objects:

return CFrame.new(math.floor(m.Hit.X/2)*2, m.Hit.Y+p.Size.Y/2, math.floor(m.Hit.Z/2)*2)*CFrame.Angles(find angle x, rot, find angle z); --rot = rotation chosen by player by pressing on r```

You could try doing a raycast from Mouse.UnitRay and using the returned surface normal to rotate your object’s CFrame. I forget which CFrame function it is to rotate using a vector, but I’ll look it up and edit this post when I find it.

Edit: maybe you could try constructing a CFrame by using the normal as upVector, calculating rightVector and backVector and plugging those in to CFrame.fromMatrix()? I’m on phone so it’s hard to show some working code

1 Like

When I come back home on studio, I’ll try doing something like this. Thanks

I believe you can do CFrame.Angles(Normal.X, Normal.Y, Normal.Z) and multiply it by any angle offset neccessary

This code should work for you, tested in Studio:

local mouse = game:GetService "Players".LocalPlayer
local targetFilter = mouse.TargetFilter
local _, hit, normal = workspace:FindPartOnRay(
    Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 5000),
    targetFilter)
local cframe = CFrame.new(hit, hit + normal)

This is actually simpler than I thought it would be :stuck_out_tongue:

I tested this in Studio, this didn’t seem to work. Do you have any working code to demonstrate a working version?

There used to be an article on the wiki about the cross product but that didn’t seem to make the migration. Regardless here’s an updated version of that article which has an example of the cross product in use to rotate an object to the surface the mouse is pointed at.

Link to article.

Hope that helps!

5 Likes

Works perfectly, changed a little your code to fit my system but yes awesome. :ok_hand:

thank you for the code, I’ve been looking for something like this all week :+1:

THANK YOU, EGO MOOSE. IV BEEN TRYING TO FIND A WAY TO DO THIS ALL WEEK. :pray: