Weird CFrame behavior

So I am creating code that places a part right in front of the players character, so I did this:

local targetCframe = root.CFrame * CFrame.new(0,0,-1)
local targetPo = targetCframe.Position

In theory this should work, but instead it puts it right next to the default spawnpad.

So I eventually tried this:

function getPO2(distance,char)

	local part = Instance.new('Part') -- the reason we make a part is cause other wise it will act weird

	local po = char.PrimaryPart.CFrame * CFrame.new(Vector3.new(0,0,distance)) 

	part.CFrame = po


	local po2 = part.Position

	part:Destroy()

	return po2
end

so the part in this code is just a placeholder, and when I set its CFrame to the cframe I’ve created and then get the parts position, well it works.

So why is it that when I get the position from the CFrame, its different from the parts position.
Its really strange as they should be the same position, but its not, setting the parts CFrame to the created CFrame and then getting its position gives us the right position that I wanted, whereas without doing it, it gives us the wrong position.

Anyhow I would like to figure this out cause creating a part every time I want to do this is a little annoying, so why does this happen?

try

local targetCFrame = root.CFrame * CFrame.new(0, 0, -1)
local targetPo = targetCFrame:PointToWorldSpace(Vector3.new(0, 0, 0))
1 Like

Sooo you want to get position in front of the player?
You can use:

root.CFrame.LookVector * Distance		--This wil return Vector3

In the first bit of code, did you make sure ‘root’ was referencing the correct part? Both methods should return thesame position (given distance = -1 in the function)

1 Like

Hmm this yields the same result as before, odd.

Yes I am 100% sure its the right part, I’ve printed it and more

Ok yup this works! I guess that the CFrame just isnt in the world space or whatever to begin with so setting the cframe of a part to it would make it go to the right place, glad to know roblox has a function built in for it!
Thanks~
~Frodev

1 Like

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