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

How to make camera follow mouse?

Asked by 6 years ago

I'm trying to make the camera that follows a focus move around like clash of clans or league of legends.

The way I initially had it was set up with key inputs but I want it to be using the mouse. Right now I'm just trying to figure out how to make the camera follow the mouse even when no button is being held down.

I've also tried current pos - orig pos for dragging.

The camera doesn't really go in the direction of the mouse I'm guessing it has something to do with world space..? Any help?

dragSpeed = 0.001

userInput.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("Moving to: ",input.Position)
move_vector = Vector3.new(input.Position.X*dragSpeed,0,input.Position.Y*dragSpeed)
end
end)

1 answer

Log in to vote
0
Answered by 6 years ago

This should give you a really good idea as to what you need to do. In your original script you aren't even referencing the camera that is why your camera isn't moving. As you'll eventually figure out, the mouse doesn't move when you hold MouseButton2 down. I'll let you figure that out.

Put this LocalScript in StartCharacterScripts

local Player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera --Get the camera so you can manipulate it
local mouse = Player:GetMouse()
local pressed = false

--Set Camera to scriptable so you have more control
cam.CameraType = Enum.CameraType.Scriptable

local function MoveCamera()
    local x,y,z = nil
    local screenX = mouse.ViewSizeX --pixels
    local screenY = mouse.ViewSizeY --pixels
    local LEFT,DOWN = -1,-1
    local RIGHT,UP = 1,1
    local move_vector = Vector3.new(0,0,0)

    --Check which side of the screen they clicked on
    --imagine     |
    --            |
    --      -------------
    --            |
    --            |

    if mouse.X < (screenX/2) then
        x = LEFT
    else
        x = RIGHT
    end
    if mouse.Y < (screenY/2) then
        y = UP
    else
        y = DOWN
    end
    z = 0 --Moves forward and backwards

    --Get the position of the camera and add your new Vector3
    move_vector = cam.CFrame.p + Vector3.new(x,y,z)
    --Set your camera to the new position
    cam.CFrame = CFrame.new(move_vector)        
end

mouse.Button2Down:Connect(function()
    pressed = true
    mouse.Move:Connect(function()       
        if pressed then
            --MoveCamera() --uncomment this to play with it
        end
    end)
    mouse.Button2Up:Connect(function()
        MoveCamera() --comment this if you uncommented the one above
        pressed = false     
    end)
end)
0
The camera follows a part. The part is move to the vectors location, I was just trying to figure out to get it to go in the direction of the mouse. ICosmicReaver 32 — 6y
Ad

Answer this question