Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to Move an Object Based on Mouse Location?

So, I already understand the basic concepts of getting a user's mouse and such, as well as some basic information about BodyMovers, but I am unsure of how to do this.

If I wanted a projectile to always move forward (away from your character), but change direction (not able to go backwards, naturally) based on the location of the player's mouse on their screen, how would I go about that? So far I have been using the Mouse.Hit.p to direct the projectile, but this simply moves it towards the physical part the mouse is pointing at. I simply want the cursor to be a guide for how the projectile will move forward (and how it will change course as its moving forward).

I know this is kind of a complicated thing to ask for help with, but I have no clue where to start with transferring a Mouse's relative location on the user's screen to a direction. Thanks in advance for anyone who can help.

1 answer

You can use the X and Y components to get the position of the mouse relative to the screen. And then you can use the ViewSizeX and ViewSizeY properties to find the mouse's position relative to the center of the screen.

local mouse = game.Players.LocalPlayer:GetMouse()
local relX = mouse.X - mouse.ViewSizeX / 2 -- Mouse X position relative to the center of the screen
local relY = mouse.Y - mouse.ViewSizeY / 2 -- Mouse Y position relative to the center of the screen

This discussion is preserved as it was. New answers, comments, and votes are closed.