Need help positioning model with PivotTo but keeping original orientation

As part of a plugin I’m making I would like a script that uses PivotTo to move a model’s Origin Position to a set position, but keeps the models original Origin Orientation.

I can get the model’s position change working fine, but it always set the Orientation to 0,0,0
For example:

WorkingModel:PivotTo(CFrame.new(10,0,0))

I think its * something after the CFrame.new,

WorkingModel:PivotTo(CFrame.new(10,0,0) * ????)

but I just cannot work it out. I have been going crazy reading posts and looking at the CFrame and pivot reference, but I always get either 0,0,0 orientation or some other random seeming output.

I’m afraid I’m an 3d artist and I just dont understand enough about CFrames to get this. If you get me the right answer, you can even get credit in my plugin! Oh big reward!

Thanks!

3 Likes
WorkingModel:TranslateBy(Vector3.new(10, 0, 0))

https://developer.roblox.com/en-us/api-reference/function/Model/TranslateBy

4 Likes

The code example on the PivotTo docs page does basically that. Here’s what it says:

	-- Move the character 10 studs forwards in the direction they're facing
	local currentPivot = character:GetPivot()
	character:PivotTo(currentPivot * CFrame.new(0, 0, -10))

Changing that to fit your needs should look something like:

	local currentPivot = WorkingModel:GetPivot()
	WorkingModel:PivotTo(currentPivot  * CFrame.new(10, 0, 0))
3 Likes

Thanks for your answers. :+1:

@Astr0Derp, the script you provided only moves the model BY 10,0,0. It does not move the model TO 10,0,0. So applying that script multiple times will move my model by 10 in the x axis each time. Not what I am after.

@Forummer Somehow in all my painful reading and attempted scripting, I had not discovered the TranslateBy command! It’s seems pretty much exactly what I need. However the script you provided also just moves the model by 10 in the x axis. But, importantly, It did give me the basic script that I could modify to make it work.

So now I have:

-- first we get the original model's pivot's CFrame.
local currentPivot = WorkingModel:GetPivot()

-- extract the  X Y and Z position components from the pivot's CFrame and set them to X Y and Z variables
local Xpos, Ypos, Zpos = currentPivot:components()

-- create a vector3 using those position variables
local OriginalVector = Vector3.new(Xpos, Ypos, Zpos)

-- translate the model by a value equal to the target position vector3 minus the original models position vector3. 
-- This translates the model by the distance between it's position and the target position. 
WorkingModel:TranslateBy(Vector3.new(10,0,0) - OriginalVector)

So partial credit to @Forummer :grin:

3 Likes

Ack! I’m sorry. Glad you were able to work out a solution.

To use the PivotTo you either need:

-- where
local newPos = Vector3.new(10, 0, 0)
local oldRotCF = WorkingModel:GetPivot().Rotation

-- do
WorkingModel:PivotTo(CFrame.new(newPos) * oldRotCF)
-- or
WorkingModel:PivotTo(oldRotCF + newPos)

The TranslateBy is a straight-forward approach for what you’ve described. That is a method of Models and only moves them with no rotation. You’d need to use another method, like SetPrimaryPartCFrame or PivotTo, if you wanted to include a rotation. Both of those will also do translation, so if you were to need a more general move+rot function, you’d likely go with one of those. PivotTo also works with Parts (not just Models), and it’s recommended over SetPrimaryPartCFrame (which also requires that a PrimaryPart be set), so it’s the best general-purpose solution for this kind of thing. You were on the right track to start with that.

13 Likes

Fantastic!!! :smiley:

Thanks so much, that script is a far neater solution than my clumsy dismantling and rebuilding of an innocent CFrame. Being a n00b at coding I didn’t know you could put a .rotation on the end of a GetPivot and get the rotation out of it. Sigh…

I’ll be using this in my plugin instead. Credit will be applied. :+1:

Next step in my plugin is to do the reverse, set the origin orientation of a model without altering it’s position. I think I can do it…

3 Likes

You should be able to use the same thing as above. It’s currently labeled new and old in places, but it doesn’t care. Just give it the pos and rot of the model/part and it should work.

I appreciate the thanks, but there is no need to give me credit. If I tried to give credit for all the help, ideas, and clarification I’ve received from the forum, the credit list would be longer than any of my scripts. Just do someone else the favor of lending a hand when you can. Best of luck with your plug-in!

2 Likes

thx for this nugget of scripting knowledge honestly I can script sort of well there is just a lot of stuff I still don’t know about like this, so please tell me where I can go to learn more.