How can I reverse this math thing?

so I have this here code

		run.RenderStepped:Connect(function()
		if ViewportFrame.CurrentCamera ~= nil and not inspectDebounce then
			local target = ViewportFrame:WaitForChild("Part")
			target.Orientation = Vector3.new(mouse.X, mouse.Y) / 4
		end
	end)

and what it does is it will rotate this brick depending on the players position but the problem is if I move my mouse left and right it will move correctly but if I move it up or down it will rotate it sideways and not up or down so how can I fix this here’s a video of the problem

as you can see when I move my mouse up and down it will rotate not like I intend for it to do

2 Likes

Try something like this:

target.CFrame = CFrame.new(0,0,0) --Position in the ViewportFrame
    * CFrame.fromAxisAngle(
        Vector3.(1,0,0), mouse.Y/4) --Rotation about X-axis
    )
    * CFrame.fromAxisAngle(
        Vector3.new(0,1,0), mouse.X/4) --Rotation about the Y-axis
    )

You may want to mess with the second argument to each of the fromAxisAngle constructors. Consider getting the offset from the midpoint multiplying by 2*math.pi and then dividing by some number like 100 (aka one full rotation for every 100 pixels the mouse is offset from the center) ~Roughly speaking.

Also, you may need to modify the rotation vectors (first arguments to the fromAxisAngles constructors to change the order the rotation is applied and maybe the Z-axis instead of the X-axis).

Sorry for not responding so fast but I tried this and I get an error is this code correct?

target.CFrame = CFrame.new(0,0,0) * CFrame.fromAxisAngle(Vector3(1,0,0), mouse.Y/4) * CFrame.fromAxisAngle(Vector3(0,1,0), mouse.Y/4)

if so this is my error

attempt to call a table value

easy fix
all the “Vector3”s should be Vector3.new

this was the fix but unfortunately the code didnt work so I went to a more simple version of the code that will only allow the rotation of up and down

target.Orientation = Vector3.new(mouse.Y) / 6

Did you try moving the mouse.Y to the third vector instead? Z?

Alright so if yall were wondering I did fix it today and heres my fix the old way I was doing was horrible because I wasnt specifying x and y’s so it was also like setting the z it was weird heres the fixed code

target.Orientation = Vector3.new(mouse.Y, 0, mouse.X / 5)

so now I set the x to the y and the y to the x i also divided the x by 5 this will slow its rotation speed down

1 Like