Help with generating angle from math.atan2

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I’m trying to make a tween between the goalkeeper’s root part and the position where the ball hits the goal line (white part generated in the video).

  2. What is the issue?
    I can’t find a way to correct the goalkeeper’s orientation relative to it’s final position.


    roblox2

  3. What solutions have you tried so far?
    After some searches in the forum, I tried using math.atan2(), however the results I got were very inconsistent. I tried switching up X and Y and negating the final value, but it also didn’t work.

if raycastResult then
		local humrp = workspace.Dummy.HumanoidRootPart
		
		local X = humrp.Position.X - raycastResult.Position.X
		local Y = humrp.Position.Y - raycastResult.Position.Y
		local Theta = math.atan2(Y,X)
		
		local goal = {}
		goal.CFrame = CFrame.new(raycastResult.Position) * CFrame.Angles(0,0,Theta)
		
		local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear)

		local tween = TweenService:Create(humrp, tweenInfo, goal)
		tween:Play()
	end
1 Like

You appear to be asking for an angle within the plane of the goal but the vectors you are using are all in world space. This might be correct if your goal plane is always the XY plane, but it will be flipped for the opposite teams’s goal. Also note atan2 assumes the traditional definition of 2D angles, where pointing down +x (as in atan2(0,1) ) is 0 radians, +y is pi/2 radians, and so on. Compared to where you have drawn theta in your pic you have the Y and X in atan2(Y, X) flipped.

Also you can avoid having to do trig here by using CFrame.lookAt(), which lets you specify an Up direction as well as a forward. You could use this to directly calculate the CFrame of the goalie’s final position using the vectors you’ve drawn without having to convert any spaces and without trig.

Thanks a lot, had some trouble setting the axis correctly but it worked after some time

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.