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

How can I move an ImageLabel with mouse movement?

Asked by 4 years ago
Edited 4 years ago

I'm currently working on a game based on MORDHAU, a game by Triternion.

One of the main elements in MORDHAU is its melee combat. I'm currently trying to have the players crosshair turn in the same direction as the mouse's movement.

I have all the GUI elements in place, all I need to figure out is how I'd approach changing the orientation of the ImageLabel with the mouse's movement, which shows the player which direction they'll swing.

Is this possible, and if so, how should I go about it?

Edit:

I've started a basic version with a horizontal and vertical response, which seems to be working fine.

userinput.InputChanged:connect(function(inputObj)
    if (inputObj.UserInputType == Enum.UserInputType.MouseMovement) then
        local dx = inputObj.Delta.X;
        local dy = inputObj.Delta.Y;
        print("UI:  (" .. 
            tostring(dx) .. ",  " .. 
            tostring(dy) .. ")"
        );
        if dx > 0 and dy == 0 then
            crosshair.Rotation = 0
        elseif dx < 0 and dy == 0 then
            crosshair.Rotation = 180
        elseif dx == 0 and dy > 0 then
            crosshair.Rotation = 90
        elseif dx == 0 and dy < 0 then
            crosshair.Rotation = -90
        end
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

To do this, you would want to use the player's mouse instead. There's a function called Move on a player's mouse.

This is how you would do it.

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

mouse.Move:Connect(function()
    script.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)
end)

This code will run whenever the player's mouse moves. We use the offset, because that value is put in 100's, not 1/100ths.

If you want for it to look smooth whenever you move it, or tween to a different position, you would do something like this.

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

mouse.Move:Connect(function()
    script.Parent:TweenPosition(UDim2.new(0,mouse.X,0,mouse.Y),"Out","Back",1,true)
end)

Same thing here, but we use TweenPosition instead.

Hope this helped! If this is what you're looking for, let me know by selecting this as an answer!

0
Thanks for the quick reply, however, this is not what I'm looking for. What I'm trying to pull off is to rotate the mouse based on the previous movement of the camera. I've found a way to kind of pull this off already, however, it's jittery and sometimes buggy. User#32778 0 — 4y
0
Oh, alright. I'll see if I can do that! killerbrenden 1537 — 4y
Ad

Answer this question