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

How to Move an Object Based on Mouse Location?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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
Ad

Answer this question