Need help with CFrame positioning

I want to be able to make the player teleport behind an object while keeping the initial facing direction towards the object the same and coming out the opposite side. (Like shown in image)

I have tried to accomplish this using CFrame.lookAt, but then can’t consistently teleport behind the object in the same direction and place.

Help would be greatly appreciated

1 Like

use cframe:lookAt to get the cframe of p facing o

then remove position from the cframe by subtracting a vector3 of the cframes position from it

then add to the cframe the vector3 position of o + the cframe’s lookvector * 5 (or whatever distance you want)

use :pivotto to move p with this newly created cframe

1 Like
local a=CFrame.LookAt(p.CFrame,o.CFrame) -- p:GetPivot() instead of p.CFrame if p is a character
a+=a.LookVector*5+o.Position
p.CFrame=a -- or p:PivotTo(a) if character

maybe its the code you mean, use the following if p is a character:

local a=CFrame.LookAt(p:GetPivot(),o.CFrame)
a+=a.LookVector*5+o.Position
p:PivotTo(a)

at least is what you said (I think)

1 Like

Yes, @BrAlMeSo_Mc , that is what I was trying to say. I think this will solve the poster’s problem.

@FIamecharmed , if you find a solution, mark this post as solved.

1 Like

Didn’t work, LookAt wouldn’t allow me to use the CFrame of p and o, so I used the position instead. PivotTo didn’t work because a is a vector3 instead of CFrame

1 Like

wait my code has an error: it’s supposed to be a Vector3 both values of CFrame.LookAt()
(there’s a third value too)

1 Like

Why not just use character:MoveTo(target.Position) where target.Position is the location you want behind the wall?

1 Like

Because it’s not the same position every time, specifically the same direction P was facing O before the teleport

1 Like

It’s hard to understand what you are doing without a script.

If it will help, here is a simple teleport gun from a tutorial:

Teleport.rbxl (55.0 KB)

It probably has the info you need. (The link to the video is in the code.)

I managed to get it working, here is the code for those who are interested:

				local p = HRP
				local o = workspace.TESTTP

				local facingdirection = CFrame.lookAt(p.Position,o.Position)
				
				--Position the character behind object
				p.CFrame = CFrame.new(facingdirection.LookVector*5 + o.Position)
				--Rotate the character so the back faces the object
				p.CFrame = CFrame.lookAt(p.Position, o.Position) * CFrame.Angles(0, math.rad(180), 0)
				
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.