You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I would like a part to follow my mouse on the x and z axis
What is the issue? Include screenshots / videos if possible!
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have looked through devforum but I could not find the topic that answered my question.
Hello guys! It has been a while but I have a question. How can I make a part follow my mouse only on the x and y axis? The point is that it should not be using mouse.Hit
I have been searching through the devforum but the only thing that I found is this.
local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
rs.RenderStepped:Connect(function()
mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
end)
Just for clearance: I am NOT new to coding, I have been coding for a while but this is something I have trouble with as I do not use mouse so much.
If the mouse moves, you fire a remote event with the mouse.Hit.Position argument. Then with a script you check if the event is fired, if yes then put the parts position to the event argument.
I believe you mean x and z axes because y axis is the vertical axis in roblox. What you are trying to do is calculating the intersection point of the line going from the camera in the direction defined by the mouse.
You can get the mouse’s screen position using UserInputService:GetMouseLocation, convert this to a worldspace direction vector with Camera:ViewportPointToRay and the calculate a multiplier for the vector by dividing the total needed vertical movement by the vertical movement defined by the vector.
local UserInputService = game:GetService("UserInputService")
local planeY: number = 0 -- change this to whatever you want
local camera: Camera = workspace.CurrentCamera
local function getMousePositionOnPlane(): Vector3
local mousePosOnScreen: Vector2 = UserInputService:GetMouseLocation()
local mouseWorldSpaceRay: Ray = camera:ViewportPointToRay(mousePosOnScreen.X, mousePosOnScreen.Y)
local origin: Vector3, direction: Vector3 = mouseWorldSpaceRay.Origin, mouseWorldSpaceRay.Direction
local multiplier: number = (planeY - origin.Y) / direction.Y
return origin + direction * multiplier
end
You may have misunderstood what my problem really is. I have already figured it all out how to change the parts position. What I want however is it follows an invisible horizontal plane.
Then you would need raycast, check if part is not hovering air.
local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
rs.RenderStepped:Connect(function()
local direction = Vector3.new(0,-5,0) -- Direction
local raycastResult = game.Workspace:Raycast(mousepart.Position, direction)
if raycastResult then
mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
end
end)
or you can also check if mouse is not on air.
local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
rs.RenderStepped:Connect(function()
if mouse.Target ~= nil then
mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
end
end)
He doesn’t seem to want it to stay on a BasePart, but instead at a constant y coordinate. This is a line-plane intersection problem.
@fancym0000, the code you posted will not give a position on the line going from the camera in the mouse direction unless the part surface happens to be at the correct height.
@davcio12344, I wrote code that should work in the general case. However, it will give an infinite/undefined result if the mouse direction happens to be parallel to the horizontal plane. What would be the desired result in this case? Should there a maximum distance or maximum horizontal distance from the camera for the point?
What does the OP want ? Stay in one line ? He just needs to update Z or X axis. Not the part the fall into the abyss ? Check mouse.Target or raycast before initializing part. I’m seriously confused
I think you should either use mouse.Target (best option i think) or raycast to check if part is hovering on air. If you use mouse.Target it would check the mouse BEFORE initializing the part, the first script about raycast will check if there is a part under the mousepart (so only when you’ll put the mousepart on the void, only after that he’ll detect it)