Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i rotate objects with scripts when you press "Q"?

I have searched youtube but i really haven't found a good example/ explanation. I have it so you can click and move and when you release stop moving it. Trying to add q to rotate it clockwise but i don't know how. Script:

--// Variables\\--

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mtarget
local down

--// Functions \\--

function ObjClick()
	if mouse.Target ~= nil then
		mtarget = mouse.Target
		mouse.TargetFilter = mtarget
		down = true
	end
end

mouse.Button1Down:Connect(ObjClick) --clicking triggers this

function mouseMove()
	if mouse and mtarget then
		local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z -- moving obj
		
		mtarget.Position = Vector3.new(posX,posY,posZ)
	end
end
mouse.Move:Connect(mouseMove)

function mouseDown()         --releasing triggers this
	down = false
	mouse.Target = nil
	mouse.TargetFilter = nil
end
mouse.Button1Up:Connect(mouseDown)

--// Rotation of obj \\--

game:GetService("UserInputService").InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.Q then
		--dont know how to rotate the object :/
		
		end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input,gameprocessed)
    	if input.KeyCode == Enum.KeyCode.E then
    		--dont know how to rotate the object Clock wise :/
    		
    		end
    end)

3 comments

0

mtarget is the object clicked

0

You can use CFrames

0

i dont know how if you could answer me and explain how i would use it in the situation that would really help. :D

1 answer

✓ Accepted answer

Code says everything in-depth.

game:GetService('UserInputService').InputBegan:Connect(function(Key, Process)
	if (not Process) then return; end --If there is a GuiObject with Modal enabled present, then..
	
	Key = Key.KeyCode -- We don't need any other values that Key gives, so we'll just assign it to it's own KeyCode.
	if (Key == Enum.KeyCode.Q) then --If the Key is equal to Q. If we needed any other values that Key itself provided, we'll do "if (Key.KeyCode == Enum.KeyCode.Q) then."
		if (mtarget) then --Just to make sure if mtarget isn't nil.
			local X,Y,Z = mtarget.CFrame:toEulerAnglesXYZ() --Get the rotations
			mtarget.CFrame = mtarget.CFrame * CFrame.Angles(math.rad(X),math.rad(Y+90),math.rad(Z)) --To get the angles, we call :toEulerAnglesXYZ(), which returns a tuple variant. Cool! Now, we can use that in the CFrame.Angles to rotate it 90 degrees, by adding 90 to the Y.
		end
	end
end)

You MUST read up on the toEulerAnglesXYZ() function if you wish to understand this properly. It's pretty rad, plus it's a good read, anyway.

We get the rotation values by using :toEulerAnglesXYZ() and assigning it to variables X, Y, and Z. We then plug those variables into math.rad so we can convert it into Radians, and then we plug the three radians to generate the sequence of CFrame.Angles(math.rad(X), math.rad(Y+90), math.rad(Z)). Pretty fancy.

Resources

https://developer.roblox.com/articles/Euler-Angles https://developer.roblox.com/articles/Understanding-CFrame

1 comment

0

lol

This discussion is preserved as it was. New answers, comments, and votes are closed.