How can I orient a player relative to a part

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make a portal that when you go into it facing a certain direction, you come out, facing the same direction relative to the new location

  1. What is the issue? Include screenshots / videos if possible!

I have only been able to make it so that it mirrors the player (the player is in first person so to rotate them I turn the camera in a local script) but if the portal number 2 is on a different rotation axis it does not work correctly, I want it so that if I go into a portal on the front wall that is linked to a portal on the side wall, it will work like this, if you are facing forward toward the portal when you go in, when you come out, you will be facing the same way relative to where you now are, but instead, it just reverses the location

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking for solutions but there is nothing
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
    This is the code I use to rotate the camera
game.ReplicatedStorage.Rotate.OnClientInvoke = function(LookPart)
	
	--lookpart is a part always facing forward relative to the portal
	workspace.CurrentCamera.CFrame *= CFrame.Angles(math.pi,math.pi,math.pi)
end

Exact problem in video form (sorry for watermark I had to remove the audio track because I forgot to mute)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

so that we can better handle the problem, make it so that when portal1 is touched, it prints the orientation of the humanoidrootpart, and when it has reached portal2, it prints the orientation of the character again, and then you tell me that it was printed

1 Like

do you mean when it rotates or before it rotates

that prints the 2, when entering portal1 (original rotation) and when reaching where portal2 is (incorrect rotation)

1 Like

sure, I will also be uploading a video of exactly what is happening

1 Like

Something I would like to know is why each axis in the angle is pi, I think that would be the reason for the problem too, since math.pi does not guess the orientation of “lookpart”

game.ReplicatedStorage.Rotate.OnClientInvoke = function(LookPart)
	
	--lookpart is a part always facing forward relative to the portal
	workspace.CurrentCamera.CFrame *= CFrame.Angles(math.pi,math.pi,math.pi)
end```
1 Like

this was so that even in cases where portals were on ceilings and floors it would still work

2 Likes

Here is the video

1 Like

--------------------------oh ok

btw, if you need to extend a message for the char limit, you can use zero width spaces

1 Like

I see, I think I could try to figure out how to do it, I’ll try to do something similar, and if you haven’t found the solution yet I’ll tell you how to do it.

1 Like

that you so much, this is really helpful

Hey, I have been finding out and experimenting, I won’t tell you that I already found the solution, but I can tell you that I am reaching a possible solution.

You could use CFrame:ToObjectSpace() what it does is it rotate given cframe relative to the new one, you even don’t need math for this, here is tutorial: CFrame | Documentation - Roblox Creator Hub

I knew that cframe had a function for that, but I didn’t know which one, could you give an example? Because I tried that but I don’t think it was the way it should be.

1 Like

i gave you tutorial for more in-depth, but describing it short, you use
CFrameA:ToObjectSpace(CFrameB) ==> NewCFrame

1 Like


I have solved it, answer me and I will immediately send you a file with the model of the 2 blocks and the script, you could also use the method that ideal recommended

OH MY GOD, YOU A GENIOUS, sadly I use linux, therefore importing rblx does not work, could you please send me the camera rotation code, but thank you so much

2 Likes

oh ok sure
Btw, there are 2 scripts, one normal and one local, and they need a remoteEvent in the replicatedstorage, and put the local script in the starterplayerscripts

SCRIPT


part = script.Parent
part2 = workspace:WaitForChild("portal2")
Event = game.ReplicatedStorage.RemoteEvent
debounce = true

function OrientationRelative(portal1,portal2,Y)
	
	local num1 = portal1.Orientation.Y - portal2.Orientation.Y
	local num2 = Y - num1
	local result = num2 - 180
	
	print(Y)
	print(num1)
	print(num2)
	print(result)
	return result
end


part.Touched:Connect(function(hit)
	
	if debounce then
		
		if hit.Parent:FindFirstChild("Humanoid") then
			
			debounce = false
			local rootpart = hit.Parent.HumanoidRootPart
			local AxisY = rootpart.Orientation.Y
			rootpart.CFrame = part2.CFrame:ToWorldSpace(CFrame.new(0,0,-4))
			local orientation = CFrame.fromOrientation(math.rad(0),math.rad(OrientationRelative(part,part2,AxisY)),math.rad(0))
			Event:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent),orientation)
			--rootpart.CFrame = CFrame.new(rootpart.Position) * CFrame.fromOrientation(math.rad(0),math.rad(OrientationRelative(part,part2,AxisY)),math.rad(0))
			wait(2)
			debounce = true
		end
	end
end)

LOCALSCRIPT

event = game.ReplicatedStorage.RemoteEvent

camera = workspace.CurrentCamera

event.OnClientEvent:Connect(function(orientation)
	
	
	camera.CFrame = CFrame.new(camera.CFrame.Position) * orientation
	
end)