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

Manipulating the Z rot of C1 with the mouse's Vertical Pos Relative to the Camera, In a fixed cone?

Asked by
vat21s 0
9 years ago

Alright, So lets begin. I need a way to make the barrel of my cannon move up and down with the script, the cannon is attached to a Boat, and it's Pivot's rotation is independent of the boat. How would I go about doing this? I also would like to be able to incorperate firing cone of about 15? from the top. So rather than a 90? Firing cone it would only be 75?

What is the correct way to Collet the Mouses Y Axis location and parse it correctly to always be between 0-75? and facing the Front face towards the end of the barrel, I also need it to be able to return the angle? What would be the C-Framing magic needed for this? Calc(int) was a method I devised as an attempt before.

--==LOCAL SCRIPT==--
--Variables
mouse=game.Players.LocalPlayer:GetMouse()
Rotor=Workspace.Model.Rotor
ScreenY=game.Workspace.CurrentCamera.ViewportSize.Y
minAngle=0
maxAngle=75
Gun=Workspace.Model.Gun

function Calc(Y)

    local Res=math.rad((ScreenY*Y/90))--Ratio'd using  Result/2Pi=90y/90 -- 90y= 90*y == ScreenY*Y 
    --If Resulting angle >75
    if Res>maxAngle then
        return maxAngle
    --If Result angle <0
    elseif Res <minAngle then
        return minAngle
    --If Result is not defying the limits
    else
        return Res
    end

end

while wait() do
        --RelativeReferenceFrame
        RotorPos = Rotor.Position
        --UnitVector of Direction
        dir = (mouse.Hit.p - RotorPos).unit
        --Setting the Offset of RotorWeld to (0,1.5,0)*(0,inverseTangent of dir.X/-dirZ + pi,0):inverse()
        --Basically rotating on the Y axis with the mouse.
        Rotor.RotorWeld.C1 = CFrame.new(0,1.5,0) * CFrame.Angles(0, (math.atan2(dir.X, -dir.Z) + math.pi), 0):inverse()
        --I still need a way to retrive the angle properly on the local reference frame.

        --offsetH=math.cos(angle)*Gun.Size.Z
        --offsetV=math.sin(angle)*Gun.Size.Z
        --Move the X Axis in this case which controls the up down movement of the barrel,
        --Set the offsets correctly.
        Gun.GunWeld.C1 = CFrame.new(0,0,0)*CFrame.Angles(0,0,0)--Im clueless e.e
end

Answer this question