Right now i’m trying to add console compatibility onto my game and i’m testing it with a PS4 controller on the ROBLOX app on microsoft store However I have a problem. The problem isn’t the registering of the userinput, it registers the trigger,
The problem is: It seems that the Console player’s “cursor” is pointing elsewhere and not in the direction of the camera’s lookvector. (Shift lock is forced in my game)
userinputservice.InputChanged:Connect(function(input, gameprocessedevent)
if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.None then
GrabHandler:ItemUpdate(input.Position.X, input.Position.Y)
CameraHandler:InputUpdate(input.Position.X, input.Position.Y)
end
end)
--On console, input.X and input.Y both return something like -0.84563 some small
--number, While on PC both return normal values, 960 ish (middle of screen)
I’m pretty sure it’s because with a mouse as the input type the X/Y coordinates are relative to the screen.
The “MouseMovement” UserInputType will fire for whichever input device is considered the “mouse”. In the case of an Xbox controller it’d be one of the thumbsticks. (It depends on your settings I’m fairly sure.) Therefore, the input X/Y would show the position of the thumbstick, which is between -1 and 1 on each axis respectively. (Full circle.)
Thank you for replying! How exactly would i implement it into my script though as I only want to get the equivelant of inputobject.position of PC on Console
.
As you can’t use the MouseMovement input event similarly as you would on PC, maybe UserInputService:GetMousePosition() might work? If it doesn’t (I assume it doesn’t, but worth a try), then the only alternative I can think of is using the camera position & lookVector directly.
function GetTarget()
local NewRay = Ray.new(camera.CFrame.Position, camera.CFrame.LookVector)
local Target = workspace:FindPartOnRay(NewRay, game.Workspace.TargetFilter)
return Target
end
CFrame.LookVector is a unit vector so it’s length is always 1, the direction of the ray also encodes the length of the ray. So in your case it’d only cast one stud into the direction of the LookVector.
Something like this should work:
local NewRay = Ray.new(camera.CFrame.Position, camera.CFrame.LookVector * 300)
That would cast a ray 300 studs into the LookVector’s direction, you can change that to suit your needs.
function GrabHandling:ItemUpdate()
if grabbing == false then
--UpdateHighlight
local Target = GetTarget()
if Target and Target.Parent:IsA("Model") and Target.Parent.PrimaryPart then
if Target.Parent:FindFirstChild("Grabbable") then
if (plr.Character.PrimaryPart.Position - Target.Parent.PrimaryPart.Position).Magnitude < dist * 1.2 and not Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
if highlightobject and Target.Parent.Outline ~= highlightobject then
highlightobject.Transparency = 1
end
highlightobject = Target.Parent.Outline
highlightobject.Transparency = 0
end
else
if highlightobject then
highlightobject.Transparency = 1
end
end
else
if highlightobject ~= nil then
highlightobject.Transparency = 1
end
end
end
end
and like this for the camera:
function CamHandling:InputUpdate()
--CAMERA LOOK
local LookPoint = GetMousePoint()
local CameraDirection = Root.CFrame:toObjectSpace(LookPoint).lookVector
if Neck then
if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
end
end
game.ReplicatedStorage.Look:FireServer(Neck.C0)
end
Aight, so the issue still lies in the GetTarget() logic. Might have something to do with the cursor being offset. Any idea what’s going on with that? Does it only happen on console (or PC) or both?
Use the center of the screen as the coordinates, and 300 as your depth/length of the ray (or any other number that you see fit), so something like this:
function GetTarget()
local viewportSize = camera.ViewportSize
local NewRay = camera:ScreenPointToRay(viewportSize.X / 2, viewportSize.Y / 2, 300)
local Target = workspace:FindPartOnRay(NewRay, game.Workspace.TargetFilter)
return Target
end
Also, another thing that might be an issue is that the player’s character might be in the way of the ray resulting in that being returned by FindPartOnRay. Try using something like FindPartOnRayWithIgnoreList and pass your target filter and the character as a table. (WorldRoot | Documentation - Roblox Creator Hub)
It creates a ray originating from the camera’s position into the camera’s direction, offset by the worldspace position of the x/y coords passed in. (In this case the screen’s center.)
So if you want it to always originate from the center of the screen, this will work on any device.
I am testing it again on Console and same problem again, The player follows camera on PC but not console except this time, no item is being found it constantly prints nil unless i pass through a non-can collide object.
is there another way i can set the depth of the ray except for this method
local NewRay = camera:ScreenPointToRay(viewportSize.X / 2, viewportSize.Y / 2, 300)
by adding 300 in the third arguement? Because i believe that’s where the problem for grabbing is at.
Ah wait, I misread the documentation, the 3rd number is the depth from the camera not the length of the ray. Remove the 3rd argument, as it returns a unit ray. Also it’s better to use ViewportPointToRay since it ignores GUI inset from things like the top bar.
local unitRay = camera:ViewportPointToRay(viewportSize.X / 2, viewportSize.Y / 2)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * 300)
One thing I don’t get is why the first method we used didn’t work anyways, shift lock shouldn’t affect it in a problematic way afaik. This method shouldn’t have to be necessary. If it still doesn’t work we might be looking at a different problem all together. Might want to use the “FindPartOnRayWithIgnoreList” method instead as it might be that the character still obstructs the ray.