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

Identifying First Person?

Asked by 8 years ago

I'm wanting to do something once a player zoom in (to the max) for example, the player zoomned in to first person and it will print 'hi' ONCE. and if he zoom out nothing will change until he zoom in back again.

0
There's nothing I could mind of as fast as you might need it, but here's the roblox wiki article for the Camera. http://wiki.roblox.com/index.php?title=API:Class/Camera Wutras 294 — 8y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

You can achieve this effect using a combination of an input event and a distance check.

Note that this code will only work in a LocalScript, so place it somewhere a LocalScript can run.

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

uis.InputChanged:connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseWheel then
        --The user is scrolling in some way, so let's check the distance between their Camera and their Head.
        --After some testing, the 'first person' distance is .5 studs, exactly. But, it's likely to be a bit higher or a bit lower at any point, so it's better to check if it's less than slightly higher than .5.
        --Because fractional studs are so small, < .6 should work:
        if (player.Character.Head.CFrame.p - workspace.CurrentCamera.CFrame.p).magnitude < .6 then
            print("First person achieved!")
        end
    end
end)

The above code isn't perfect (it will print when you zoom out as well as in, and will continue to print if you 'zoom in' while already in first person), but should be a good starting point.

Given that I know you're new to ROBLOX scripting, take a look at wiki.roblox.com, and the Object Browser in Studio, to see the API you have available.

Ad

Answer this question