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

How do I rotate an object on one axis?

Asked by 1 year ago
Edited 1 year ago

So I have been making this Dead Space Plasma Cutter type thingy and I ran into this issue where I have been trying to rotate the blades of the gun on one axis while all the other axis' aren't affected (or actually rotate). I don't know how to make this work so any and all help will be largely appreciated.

All code and sources are below to get an easier understanding of what I'm trying to accomplish.

For quick clarity I'm using a mesh part, Blades, that welds every part that's supposed to rotate upon pressing the space key. And that mesh part is motor6D welded to the Handle mesh part.

The hierarchy: https://gyazo.com/0f3c23304baddf2b0dcac5a90d471f82

https://gyazo.com/79abfd4ac96598ea10d1d95a36a66524

Code I have been using to rotate the 'Blades':

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

local plasmaCutter = script.Parent
local handle = plasmaCutter:WaitForChild("Handle")
local blades = plasmaCutter.Blades.Blades

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Space then
        local currentRot = blades.Orientation
        local targetRot = Vector3.new(currentRot.X, currentRot.Y, currentRot.Z - 90)
        TS:Create(blades, TweenInfo.new(0.1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Orientation = targetRot}):Play()
        print("Alt fire!")
    end
end)

What it should look like (while being able to move ofc): https://gyazo.com/55d733a945b9ed5afe6737f46709162a

The problem: https://gyazo.com/1b92741704ff49ee36d496feb53da891

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Welp, I figured it out! All I did was change the hierarchy a bit to have another part basically holding the position of the blades. Then I changed the code to this:

-- Creating a new Moter6D joint to rotate the blades on one axis
local bladeJoint = Instance.new("Motor6D")
bladeJoint.Part0 = blades
bladeJoint.Part1 = rotPoint
bladeJoint.Name = "BladeRotation"
bladeJoint.C0 = CFrame.new()
bladeJoint.C1 = CFrame.new()
bladeJoint.Parent = blades

local rotated = false -- Checking to see if the Plasma Cutter has already been flipped when alt-fired

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Space and rotated == false then
        rotated = true

        local currentRot = bladeJoint.C0
        local targetRot = currentRot * CFrame.Angles(0, 0, math.pi/2) -- Using CFrame calculations to rotate the blades 90 degrees to the right
        TS:Create(bladeJoint, TweenInfo.new(0.2, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {C0 = targetRot}):Play()
    elseif input.KeyCode == Enum.KeyCode.Space and rotated == true then
        rotated = false

        local currentRot = bladeJoint.C0
        local targetRot = currentRot * CFrame.Angles(0, 0, -math.pi/2) -- Also using CFrame, but to rotate the blades 90 degrees back to the left
        TS:Create(bladeJoint, TweenInfo.new(0.2, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {C0 = targetRot}):Play()
    end
end)

adding a Motor6D element in conjunction with CFrame. Finally achieving the animation with no weird rotations!

https://gyazo.com/fef01151b819e7f50cffebd42c687547

Ad

Answer this question