A PivotTo() Problem

I making some sort of card game that makes you hold the cards when you’re playing the game and I tried offsetting the pivot so it’ll seem like you’re actually holding the cards.

In Theory, it is supposed to be like this:

But it ended up like this:

Here’s the code I believe there’s a problem in:

function RePositionCards() -- Self Explanatory
	for i, v in pairs(Cards:GetChildren()) do
		local CardPosition = CFrame.new(Vector3.new(v:GetPivot().Position.X, v:GetPivot().Position.Y, v:GetPivot().Position.Z))
		local Angles = CFrame.Angles(math.rad(v:GetPivot().Rotation.X), math.rad(v:GetPivot().Rotation.Y), math.rad(i/(#Cards:GetChildren()+2)*180))
		v:PivotTo(CardPosition * Angles)
	end
end

Any help is appreciated :slight_smile:.

1 Like
math.rad(i/(#Cards:GetChildren()+2)

why +2 here? im not the best w maths and angles but thats what i looked at the most

Some things to dissect here:

local CardPosition = CFrame.new(Vector3.new(v:GetPivot().Position.X, v:GetPivot().Position.Y, v:GetPivot().Position.Z))

This is a lot of redundancy, you can just do:

local pivot = v:GetPivot()
local CardPosition = CFrame.new(pivot.Position)

You’ll also see that I localized the pivot call. Try to avoid calling functions multiple times in the future, because a simple variable lookup will always be faster than a function call!

Next is the actual problem, which is the rotation. Assuming what you have set up, you can just add a constant to your Z rotation to give all of your cards a relative rotation around the pivot:

local Angles = CFrame.Angles(math.rad(pivot.Rotation.X), math.rad(pivot.Rotation.Y), math.rad(90) + math.rad(i/(#Cards:GetChildren()+2)*180))
1 Like

Yeah, I supposed to be +1 But it is so one of each card the cards are divided by the amount of cards plus 1, then is multiplied by 180 degrees.

Example:

if there are 3 cards

1/(3+1) = .25
.25*180 = 45

2/(3+1) = .5
.5*180 = 90

3/(3+1) = .75
.75*180 = 135

4/(3+1) = 1
1*180 = 180

This is really great advice, but I still get the same result somehow.

Something tells me that I used wrong type of constraint and used the weld constraint instead of something else but I don’t know.

Cards.ChildAdded:Connect(function(child) -- "Cards" is a folder for cards
	child:PivotTo(Hand.WorldCFrame) -- The "Hand" is an attachment
	child.WeldConstraint.Part1 = RHand
	RePositionCards()
end)

A normal Weld instance is more practical for your case I’d like to say. Not to say that weld constraints are bad or anything, it’s just a weirdly different setup that has some pros and cons compared to Welds and Motor6Ds.

1 Like