Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i rotate a model?[unsolved]

Asked by 8 years ago
player = game.Players.LocalPlayer
mouse = player:GetMouse()
x=1
y=1
z=1

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        print'r' -- just here cause
        player.Character.short:FindFirstChild("Model"):SetPrimaryPartCFrame(
CFrame.new(x,y,z),CFrame.Angles(0,math.pi,0))
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

where short is the name of a message.

sadly, the above script does not work. It's supposed to rotate the model whenever i press r. But what happens is that the model moves a little bit lower, presumably to the y position of pi. So how do i have the model be rotated?

0
Are you Using CFrame or Position to rotate the model? GeezuzFusion 200 — 8y
0
edited ScriptsAhoy 202 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

SetPrimaryPartCFrame doesn't take two arguments. You need to multiply your CFrames together to apply the rotation, like this:

player.Character.short:FindFirstChild("Model"):SetPrimaryPartCFrame( CFrame.new(x,y,z) * CFrame.Angles(0,math.pi,0) ) )

(Further reading on creating rotated CFrames)

0
this does pretty much the same thing as before ScriptsAhoy 202 — 8y
Ad
Log in to vote
0
Answered by
DevSean 270 Moderation Voter
8 years ago

What Dakota said is correct however you want to remove the x, y and z variables you created and do the following instead:

local model = player.Character.short:FindFirstChild("Model")
model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame * CFrame.Angles(0,math.pi,0))

This sets the model to it's current CFrame + 180 degrees (math.pi).

If you want to rotate by 90 degrees you can do math.pi/2 or math.rad(90)

Here is how you would do it using the mouse:

local model = player.Character.short:FindFirstChild("Model")
model:SetPrimaryPartCFrame((model.PrimaryPart.CFrame - model.PrimaryPart.CFrame.p + mouse.Hit.p) * CFrame.Angles(0,math.pi,0))

You get the current CFrame of the model and minus it's position, add the mouses position and then rotate.

0
ok, ok. Lets say i set the model CFrame with: player.Character.short:WaitForChild("Model"):SetPrimaryPartCFrame(mouse.Hit.p) .......and it does rotate, but when i update it's position with the aforementioned line of code, it will go back to it's original position, so how do i get it say at the rotation? ScriptsAhoy 202 — 8y
0
i meant that it will go back to its original rotation, the positioning is fine ScriptsAhoy 202 — 8y
0
Edited my answer for you. DevSean 270 — 8y
0
when i run that i get this: attempt to multiply a Vector3 with an incompatible value type or nil ScriptsAhoy 202 — 8y
View all comments (4 more)
0
Missed off a bracket, should be fixed now! DevSean 270 — 8y
0
this does the same thing as mine, it puts the model into the floor a little, and does not rotate ScriptsAhoy 202 — 8y
0
You do realise math.pi is 180 degree rotation and you probably might not notice it, change the rotation from math.pi to math.rad(45) DevSean 270 — 8y
0
don't worry about it, GoldenPhysics solved the problem ScriptsAhoy 202 — 8y

Answer this question