Aligning a part's RightVector to the position of another part

I’ve been trying to modify the magnitude of a curve force on a football. I’ve only gotten the direction so far:

local function GetCurveForce(ball: BasePart, limb: BasePart) -- returns the magnitude of rotational force to the ball. 
	local direction = CFrame.new(limb.Position * Vector3.new(1, 0, 1), ball.Position * Vector3.new(1, 0, 1)).LookVector -- gets lookvector for comparison
	local rightvector = humanoidrootpart.CFrame.RightVector
	local diff = -direction:Dot(rightvector) -- get dot product of direction and rightvector to get direction of curve
	if diff > 0 then -- decides which direction to curve
		diff = 1
	else
		diff = -1
	end
	local curveforce = ((diff) * (power))
	return curveforce
end

I simply add the force to the ball like this:

local curveforce = GetCurveForce(ball, character["Right Leg"])
ball.AssemblyAngularVelocity = Vector3.new(0, curveforce, 0)

I want to modify this so that the curveforce is higher or lower depending on how close to the edge the player hits the ball.

A potential solution for this is to align the player’s rightvector to the position of the ball like this:


The further the player is from the ball, the “more” rotation is applied.

I’m stuck at this point. Other solutions are appreciated too!

I believe you meant torque, force when it’s rotational is called torque.

Adding a distance factor should be enough like so:

local XZ_Vector = Vector3.new(1,0,1) --Keep the distance measured on a 2d XZ plane
local function GetCurveForce(ball: BasePart, limb: BasePart) -- returns the magnitude of rotational force to the ball. 
	local direction = CFrame.new(limb.Position * Vector3.new(1, 0, 1), ball.Position * Vector3.new(1, 0, 1)).LookVector -- gets lookvector for comparison
	local rightvector = humanoidrootpart.CFrame.RightVector
	local diff = -direction:Dot(rightvector) -- get dot product of direction and rightvector to get direction of curve
	if diff > 0 then -- decides which direction to curve
		diff = 1
	else
		diff = -1
	end

--Measure distance from a birds eye view,
local torqueRadius = (ball.Position - limb.Position)*XZ_Vector.Magnitude
torqueRadius  = math.max(1,torqueRadius)
--apply distance factor
	local curveforce = ((diff) * (power)) * torqueRadius 
	return curveforce
end

However if you notice if you are at 0 distance there would practically no Torque.

to solve this there are multiple ways, you could perform add a flat value or math.clamp the value or even do the thing below to transform 0 into a value like 1. The simplest should be math.max to choose the biggest number.

Lot’s of ways to adjust this number.

2 Likes

Thanks! I’ll tinker with this. I’ll mark this as the solution if it works :smiley:

Having no torque at 0 distance is actually not a problem for me.

The idea is if the player hits the ball in the middle, it shouldn’t curve at all. I just removed that line and it works great!

1 Like