Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I find a position in world space given an initial position, distance, and rotation?

I am trying to set up a bodyPosition so my player moves to the direct left or right of the camera's orientation when a button is pressed. I think I may need some trigonometry, but I'm not fully sure. I don't have the best recollection of my linear algebra classes, so i'm wondering if someone can push me in the right direction (I don't work with math a lot so I honestly don't know what im doing).

What I Currently Have:

  • the camera's Y rotation (in radians, if i should change it to degrees lemme know)

  • the distance the player would travel

What I Need:

  • the end position

local function yOrientation(obj)
	if obj then
		local cframe = obj:ToWorldSpace()
		local x,y,z = cframe.Angles
		
		return y
	else
		return nil
	end
end

function physics:LinearThrust(forceType,D,P,maxForce,distance,camCFrame)
	print("thrusting!")
	if forceType == "BodyPosition" then
		local initPosition = self.hrp.Position -- returns vector3
		local rotation = yOrientation(camCFrame) or yOrientation(self.hrp) -- returns radian relative to world space
		
		local thrust = Instance.new("BodyPosition")
		thrust.Parent = self.hrp
		thrust.D = D
		thrust.MaxForce = Vector3.new(maxForce,maxForce,maxForce)
		thrust.P = P
		
		-- with initial position and rotation, you need to find the end position
		
	end
end

2 comments

0

If I answered your question, please mark it as solved. Glad it works great

0

sorry, thought I already did

1 answer

✓ Accepted answer

You need to be moving the character in the CurrentCamera's axis because you want to move your character according to where the camera is pointing. Firstly, we should make a value that says "I am at the character's position, but pointing in the camera's direction", then we can shift that value by an offset and move the character to that position. Everything needs to be relative to the camera.

distance = camera.CFrame - root.Position --root would be HumanoidRootPart, camera would be CurrentCamera

distance is the distance from the camera to the root (HumanoidRootPart). This is important because we need to factor in this to the current camera's CFrame to tell it to move to the character's position, but keep its orientation (using the Inverse function).

local offset = CFrame.new(-5, 0, 0) --5 studs to the left
local distance = camera.CFrame - root.Position
--move the camera's cframe by an offset, then tell it to travel -distance to reach the character's position (and keep the camera's orientation)
root.CFrame = camera.CFrame * offset * distance:Inverse()

I hope this helped.

1 comment

0

It works great!

This discussion is preserved as it was. New answers, comments, and votes are closed.