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

Vector3 relative to a another position?

Asked by 6 years ago

So I'm working on a pet system, and I've managed to get a part to follow the player and so on, but it overlaps with the players HumanoidRootPart as the BodyPosition is set to it. How would I make it so that it is relative to the player so that no matter what orientation they are facing, the pet will be 3 studs back and 2 studs to the left of the player?

Pet.BodyPosition.Position = Char.HumanoidRootPart.Position

This is what I am current using, there's a loop to maintain this.

2 answers

Log in to vote
1
Answered by 6 years ago

Hello, for manipulating sucessfully the position of a part (Which we will call "main") relatively to another one (Which we will call "base") we can use the orthonormal vectors of base's CFrame and operate with them, using scalars to reach the target point. All the 3 orientation vectors are constantly connected and updated to base's CFrame, so it will always give the expected result based on its current position.

local main, base = --> main and base parts here
local back, left = base.CFrame.lookVector * -1, base.CFrame.rightVector * -1; --> A negative scalar will always give a vector facing the opposite side
local MoveToBack, MoveToLeft = back * 3, left * 2;
local result = MoveToBack + MoveToLeft; --> The sum of the vectors will generate a new one, based on their magnitudes
main.CFrame = CFrame.new(result);

We just used scalars and sums to reach the objective.

Ad
Log in to vote
0
Answered by 6 years ago

Use CFrames to achieve relative translations(position movements/displacements)

https://www.reddit.com/r/robloxgamedev/comments/8nw9ag/cframes_the_absolute_need_to_know/

Answer this question