#how can i make a part follow the mouse

286 messages · Page 1 of 1 (latest)

raw valve
#

you need to get the mouse position and then every frame you need to move the part to the mouse position

#

i made this ```lua
local uis = game:GetService("UserInputService")
local runservice = game:GetService("RunService")
local camera = workspace:WaitForChild("Camera")

local part = workspace:WaitForChild("MovingPart")

local function mouseRaycast(blacklist)
local mouseposition = uis:GetMouseLocation()
local mouseray = camera:ViewportPointToRay(mouseposition.X, mouseposition.Y)

local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
raycastparams.FilterDescendantsInstances = blacklist

local raycastresult = workspace:Raycast(mouseray.Origin, mouseray.Direction * 1000, raycastparams)

return raycastresult

end

runservice.RenderStepped:Connect(function()
local result = mouseRaycast({part})

if result then
    part.Position = result.Position
end

end)

slate glade
#

no need for raycasts you can use cframes

#

one line

raw valve
slate glade
#

atleast positioning it

#

yeah CFrame.lookat()

#

or CFrame.new

raw valve
#

how would you blacklist it the part though

#

if you dont then it will just fly into ur screen

slate glade
#

Wait is he making the part follow the mouse and not just rotating it? if so then raycasts are perfect are that

#

im a bit confused on what hes tryna say

raw valve
#

im so stupid

#

i thought he meant actually following the mouse]

slate glade
#

@vagrant dirge are you trying to make the part go to the mouse or have it look at the mouse

raw valve
slate glade
#

using cframe is perfect for making the part look at the mouse, whilst raycasts are a good way to have the part follow the mouse

raw valve
#

he means rotation

slate glade
#

so cframes

raw valve
#

yeah

slate glade
#

figured

raw valve
vagrant dirge
#

basically like an attached gun

raw valve
#

Dont use raycasts

slate glade
#

best way to do that is to use CFrame.lookat() aslong as its not multiple parts it should be fine

raw valve
#

I thought you were tyring to do something else

slate glade
#

you could also use CFrame.new(), you can do either

vagrant dirge
#

there's a base part

slate glade
#

alright, weld all the part to one part and try and use that part

#

or you can make a for loop and do the same to each part

vagrant dirge
#

well then if i would weld them they couldnt rotate

#

i could use uhh

#

i forgor the name

#

hinges

#

but the biggest problem is i do not know how to write the script

slate glade
#

hinges arnt good if your making it into a full blown pvp game

#

they are fixes based meaning anything can screw it up

vagrant dirge
#

hold on lemme open studio

slate glade
#

they are also semi advanced to get hooked up properly

raw valve
vagrant dirge
slate glade
#

kk

vagrant dirge
#

it moves up and down

slate glade
#

so a rotating gun?

vagrant dirge
#

yes

slate glade
#

almost like a helicopter blade?

vagrant dirge
#

kinda

slate glade
#

hmmmm

vagrant dirge
#

but can only go up and down

#

i also have an idea for one that can move left and right as well

slate glade
#

i dont recommend using them

vagrant dirge
#

well the game will basically be a bunch of zombies charging or something like that

#

so obviously guns would need to go up and down as well

slate glade
#

yeah thats why you just make them look at the mous

#

e

vagrant dirge
#

well how do i do that

slate glade
#

cframes

vagrant dirge
#

i dont know how to write the script

#

that's the problem

#

also here's another one

#

this one can move left and right as well

slate glade
#

CFrame.lookat(looker, lookat)

vagrant dirge
#

wtf is that supose to mean

#

i get the lookat part

slate glade
#

litterly the line you need to right to make it lookat the part

#

if you dont understand cframes i suggest you research them

#

very usefull

vagrant dirge
#

i understand cframes

#

location, size, orientation

#

x y z

#

what am i supose to write in the brackets

slate glade
#

brackets?

vagrant dirge
#

the ()

slate glade
#

looker, lookat

vagrant dirge
#

omfg

#

what is that supose to mean

#

i dont know how to write the script

slate glade
#

what do you think

#

gimme 10 minutes ill send you the script

vagrant dirge
#

ok, thanks

#

wait, i think i have an idea

slate glade
#

hmm?

vagrant dirge
#

to make the rotation easier

slate glade
#

what is it

vagrant dirge
#

hold on drawing it

slate glade
#

kk

vagrant dirge
#

the rotating part is a square

slate glade
#

if you weld everything to the red part, you could use cframes

#

both base and secondary part

vagrant dirge
#

why would i weld everything to the red part, including the base

#

then the base would rotate as well

slate glade
#

if its not welded with any constraint then it will fall off

vagrant dirge
#

well i was thinking of making the red part anchored

#

but then i think it couldnt work

#

but with a weld to the base it wouldnt work either

slate glade
#

dont anchor anything with a tool

#

it would

vagrant dirge
#

its not a tool

#

so, i can anchor the red part

#

or?

slate glade
#

OH

#

i thought it was a tool

vagrant dirge
#

nah

slate glade
#

yes you can anchor the base then

vagrant dirge
#

ok, so i can also anchor the red part

#

it would still rotate, right?

slate glade
#

nah anchor the base

#

not red part

vagrant dirge
#

well the red part isnt attached to the base

slate glade
#

oh

vagrant dirge
#

if i attach the red part with the base with a weld, then it would also become anchored

slate glade
#

yeah

#

well it would still move if it was attached to the base

vagrant dirge
#

hm, i think i have another idea

slate glade
#

and the base was anchored

vagrant dirge
#

lemme draw it

slate glade
#

k

vagrant dirge
#

just one hinge in the middle

#

therefor it attaches it to the base

#

but it can still rotate

slate glade
#

calculating hinge rotation requires math that i dont even understand

vagrant dirge
#

no no, the hinge doesnt need any math

#

it's like a weld, but rotateable

slate glade
#

what is the entire thing doing

#

ik what it is

vagrant dirge
#

do you mean the hinge?

swift jasper
#

studio** You are now Level 7! **studio

vagrant dirge
slate glade
#

yes

vagrant dirge
#

so

#

the hinge doesnt affect our script

#

it actually makes it easier

#

it attaches the rotating part to the base

slate glade
#

im not good with physics

vagrant dirge
#

its nothing about physics

slate glade
#

if it doesnt require scripting why are you putting this in scripting help

vagrant dirge
#

bruh

#

when did i say it doesnt require scripting

#

i need the part to follow the mouse

#

which requires scripting

slate glade
#

it doesnt need a hinge then

vagrant dirge
#

the hinge doesnt require extra scripting

vagrant dirge
#

how many times do i have to explain this

#

lemme draw more

slate glade
#

you dont need a hinge

#

im telling you

vagrant dirge
#

dude

slate glade
#

you DONT need a hinge

vagrant dirge
#

you need a hinge

slate glade
#

you really dont

vagrant dirge
#

let me draw the explination

slate glade
#

ask any other scripter here

vagrant dirge
#

just wait a bit

slate glade
#

thats what im saying

#

you dont need a hinge

#

you need cframes

vagrant dirge
#

did you even read what i fucking wrote

#

so, do i anchor the red part?

slate glade
vagrant dirge
#

DUDE

slate glade
#

im trying to help you

vagrant dirge
#

WHAT AM I SUPOSE TO DO

slate glade
#

im saving you so much time by telling you to use CFRAMES

vagrant dirge
#

I DONT WANT THE PART TO GO TO THE CURSOR

#

I WANT TO TO ROTATE TO THE CURSOR

slate glade
#

OK

#

I KNOW THAT

#

you litterly said this is what you want

vagrant dirge
#

THE ONLY CFRAME WE WILL BE USING IS ORIENTATION

slate glade
#

if your not gonna stick to the same fucking thing then dont expect answers

slate glade
vagrant dirge
#

cframe isnt only orientation

#

its also size and position

slate glade
#

it pretty much is

#

its rotating and orientation mostly

#

sure you move the part but you set it to look at something

#

your always doing something with orientation when working with cframes

vagrant dirge
#

so

#

if i dont anchor the red part

#

how do i attach it so that it doesnt fall

slate glade
#

you anchor the base and weld the red part to it

#

you use welds

vagrant dirge
#

holy shit dude

#

welds

#

dont

#

allow

#

rotation

slate glade
#

they do

vagrant dirge
#

tell that to any modeler

slate glade
#

litterly bet

#

ive done it before

#

Welds dont fuck with cframes/rotation

vagrant dirge
#

i think i know what you want to do

#

but i think it wont take other parts with it

slate glade
#

it will

vagrant dirge
#

and it wont be very smooth

#

ok

slate glade
#

you can make it smooth

#

just update it using run service

vagrant dirge
#

wtf is even that

slate glade
#

bruh

#

tween service then

vagrant dirge
#

i dont know how to use that either

slate glade
#

bruh

vagrant dirge
#

im not good at scripting in case you havent notice

slate glade
#

mhm

vagrant dirge
#

im good at certain things

slate glade
#

if you use hinges it wont work and if it does you have to spend hours and hours

#

if your making something like a helicopter blade then go for hinges

vagrant dirge
#

i know more than one game that have what i want to make and work with hinges

#

destory the island and destroy the ship

#

they both use hinges

slate glade
#

what are you wanting to do at this point because youve comepletely abandoned what you have said originally

vagrant dirge
#

just tell me your plan on how to script this thing

slate glade
#

how can i tell you my plan if i dont even know what im scripting

vagrant dirge
#

i guess every single thing that i just said doesnt fucking matter

#

2nd time im sending this drawing

#

ok, lets do without hinge

#

i honestly dont know how this would work without the hinges

slate glade
#

cframes like ive said

#

aslong as shits welded hinges arnt needed

vagrant dirge
#

ok

slate glade
#

weld the base and red part toghether and blue part and red part toghether

vagrant dirge
#

ok

slate glade
#

then use cframes to do the whole rotate to mouse thing

#

using scripts

vagrant dirge
#

do we like use bodyforces, bodyvelocities?

#

forgot to mention those are also cframes

slate glade
#

they really arnt

#

no bodyforces

#

alls you need in the workspace is the base red part and blue part welded all to the red part

#

and the bottom part anchored

vagrant dirge
#

yeah

#

kinda figured that out, cuz i model, a lot

#

it doesnt looks good

#

but i still do it

#

btw there's another problem

#

the player sitting on a certain sear can control it

#

cuz, there will be multiple guns and stuff

slate glade
#

is it supposed to be automatic or player controlled

vagrant dirge
#

player controled

#

when somebody goes onto that seat they can control the gun

#

the bullet thing is not a problem

#

it's simple really

slate glade
#

so why is a problem then

#

if tis supposed to be player controlled

#

or are you asking how to make it player controlled

vagrant dirge
#

the thing is

#

i dont know how to make it so that you can control where the gun points after you sit

slate glade
#

you would use the mouse

#

or keys

vagrant dirge
#

holy fuck this is going nowhere

#

obviously the mouse

#

but

#

i dont know how to make it so that you can control once you sit in that particular seat

slate glade
#

remote events

vagrant dirge
#

i have never used them 👍

slate glade
#

just sends a signal to another script

vagrant dirge
#

ok but how do i do tha

#

that*

slate glade
#

use a while true do loop to constently detect if someone is setting

vagrant dirge
#

how do i detect that somebody is sitting

slate glade
#

its a property on the seat

vagrant dirge
#

named?

slate glade
#

or you could use get property changed signal

#

idk its there somewhere

#

it should be obviouse

vagrant dirge
#

i need to know who the player sitting is

#

so that they can control the gun with their mouse

slate glade
#

ik thats what is says

swift jasper
#

studio** You are now Level 11! **studio

slate glade
#

the sitters parent would be their character

#

or the sitter itself would be the humanoid

vagrant dirge
#

i have no idea what any of that means

slate glade
#

bruh