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

Why is the y axis for PlayerMouse always moving up?

Asked by 8 years ago

Inside of a local script in a hopperbin.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

if script.Parent.Main.Disabled == true then
script.Parent.Main.Disabled = false
local ghost = Instance.new("Part", game.Workspace)
ghost.Name = "Ghost"
ghost.Anchored = true
ghost.Size = Vector3.new(4,4,4)
ghost.Color = Color3.new(20,50,100)
ghost.Transparency  = 0.5
ghost.CanCollide = true

while true do
    wait(0.05)
    ghost.Position = Vector3.new(math.floor(mouse.hit.x / 1), math.floor(mouse.hit.y / 1)  , math.floor(mouse.hit.z / 1))
end

When I am using this to make a cube follow my mouse the cube keeps going up, I have removed the CFrame value for y and replaced it with a 0, that works but it messes up because it ends up in the ground (because it is CanCollided) But if I set CanCollide to false then it messes up the other script in the hopper bin(Doesn't break it, just makes the output of that script turn out the wrong way)

1
Is it only moving up? From that code it looks like it should be quickly moving towards the camera rather then just up. Prohibetur 70 — 8y
0
Well, it is going towards the camera but it is up.. Not up in a straight line though. Is there a different way to do this? legoguy939 418 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

What is happening is that the mouse is touching the outside of the part and the parts position is the inside of it. It's rather hard to explain; so I will use this image. A way to get around this is by ray casting. This may be a little bit more complicated but it will allow the mouse to ignore the part when deciding where to put the part. So the first step is creating the ray which is done using this line of code:

local start = Vector3.new(workspace.CurrentCamera.CoordinateFrame)--Starts the ray at the camera position
local lookAt = Vector3.new(mouse.Hit)--Where the ray will be pointed(Not where it will end)
local ray = Ray.new(start, (lookAt - start).unit * 999)--The 999 is how long the ray will go for. Roblox has a limit of 1000 studs however.

Next is finding where on the ray it hits a part, but ignoring the Ghost Part. This can be done with

local hit,pos=ray:FindPartOnRay(ray,ghost)--hit is the part it touched, and pos is where it touched

There is more information on FindPartOnRay here.

There is more information on Rays here.

0
Thank you, very much! legoguy939 418 — 8y
Ad

Answer this question