How to get rotation difference using ToObjectSpace

I am not really sure what code I should try, so for now, I am using ToObjectSpace.
The “Y” direction is like this right?

Anyways,
I have A NPC model, that when I press R, it rotates 45 degrees in the (I think) Y Direction. It works nicely on most surfaces, but on uphill surfaces, The NPC rotates On the Y axis (usually good) but then The NPC’s legs, are like this (the green parts are the legs)

I want this.


How would I achieve this?

1 Like
function RotNPC ()

	local NPC = --patch to NPC model
	local currentPivot = --patch to NPC model:GetPivot()

	NPC:PivotTo(currentPivot + Vector3.new(0, 0, 45))



end
			-- also sript / promt triggered :Connect(RotNPC)

Try this

Wouldn’t that make the NPC’s cCFame 45 studs higher?

Sorry, I got confused with the Z axis
(I changed the script, you can try again)

That would just move the NPC, I want him to rotate along the Y axis of a part.

Here is a video of what it does now.

When It was on the grass, It worked how I would like it to.
When it was on the gray part, it did not work as intended.
Do I need to show my code?

Unfortunately, I’m going to sleep now (it’s almost night time), but the problem is probably that you need to make small invisible ones
like steps on the slope so that the tower can stand flat (It looks like something on TDS)
image
image

This is not an ideal version, it is something similar to my idea

Yes, but I would like it to align with slopes

Does anyone else have advice? My NPC rotation still doesn’t work.

You can try to make in your script that the position of NPC Z (or Y) will be constant all the time, which will not allow the NPC to change the angle of inclination

Assuming your using raycast’s to position the npc model, use the normal vector to determine which direction points up and use it to align the npc

I am teleporting the model to the mouse position/hit every .05 seconds, I want the base (The blue part ) to always be on top, or an even distance to the top. In the video, a second before it ended, you could see all of the base evenly spaced under the grey part. You know when you rotate a model, and Press L to rotate it based on It’s current rotation? How would I get the Grey part’s rotation, and apply it to the NPC’s rotation?

Okay, I got that done, but when I press R a second time the NPC’s Y axis rotates. How to I get the direction the HumanoidRootPart of the NPC is looking at?

Could you show the code your using to rotate and position the npc?

When a player presser R-

	local MouseTarget = Mouse.Target
		local MouseHit = Mouse.Hit
			local Target = 	Plr.TroopTargetUnfinishedPlacement.Value--The model
--Gets the models orientation, and the value of the wanted to increase rotatation
			local TargetX = math.rad(MouseTarget.Orientation.X + Target.XRotation.Value)
			local TargetY = math.rad(MouseTarget.Orientation.Y + Target.YRotation.Value)
			local TargetZ = math.rad(MouseTarget.Orientation.Z + Target.ZRotation.Value)
			
		
			local NewPart = Instance.new("Part")
			NewPart.Anchored = false
			Target.YRotation.Value += IncreaseValueRotationValue
		 	NewPart:PivotTo(CFrame.new(MouseHit.Position) * CFrame.Angles(TargetX,TargetY,TargetZ))
			local PartCFrame = NewPart.CFrame
			NewPart:Destroy()
			
			
			CFrame.Angles(TargetX,TargetY,TargetZ) )
			Target:PivotTo(PartCFrame)

I recommend you to use raycast’s instead as it gives you a normal vector to work with to easily align itself with the part’s surface

I was about to post the same thing. Basically, you can raycast downwards from some part by raycasting from part.Position to part.CFrame.UpVector*-5 or something like that. Then, the raycast result table has something called “normal” it just gives you the unit vector that is perpendicular to the ground. You can then use SetPrimaryPartCFrame accordingly on the npc to get it to rotate correctly.

Do you have an example of could I could use? I am still somewhat confused. So I raycast up to the NPC from the part that the NPC is on, then I get a “normal” value?

Raycast down from the NPC. I need to know the formatting of your npc in order to know the exact thing to raycast from.

I have a R6 blocky npc, on a part that is rotated 45 degrees. I am not sure what else to put.

An explorer screenshot would help a bit. Just basically raycast from the npc’s main body downward at the ground.