Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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
0
If I answered your question, please mark it as solved. Glad it works great pidgey 548 — 5y
0
sorry, thought I already did BronzedMocha 60 — 5y

1 answer

Log in to vote
1
Answered by
pidgey 548 Moderation Voter
5 years ago

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.

0
It works great! BronzedMocha 60 — 5y
Ad

Answer this question