Skip to content

Commit

Permalink
Patch setting SmoothRotation config
Browse files Browse the repository at this point in the history
  • Loading branch information
fewkz committed Nov 4, 2022
1 parent 256b34b commit eaabb74
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can also add realism to your project via [Wally](https://wally.run/) by
adding the following dependencies.

```lua
CharacterRealismClient = "fewkz/[email protected].0"
CharacterRealismClient = "fewkz/[email protected].1"
CharacterRealismServer = "fewkz/[email protected]"
```

Expand Down
47 changes: 23 additions & 24 deletions client/src/FirstPersonCamera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ local function isHeadAttachment(attachment: Attachment)
or attachment.Name == "HatAttachment"
end

local function isInFirstPerson()
local camera = workspace.CurrentCamera
if camera and camera.CameraType ~= Enum.CameraType.Scriptable then
local focus = camera.Focus.Position
local origin = camera.CFrame.Position
return (focus - origin).Magnitude <= 1
else
return false
end
local function isInFirstPerson(camera: Camera)
return if camera.CameraType ~= Enum.CameraType.Scriptable
then (camera.Focus.Position - camera.CFrame.Position).Magnitude <= 1
else false
end

-- Wraps BaseCamera:GetSubjectPosition() to offset the camera
Expand All @@ -36,7 +31,7 @@ local function wrapGetSubjectPosition(getSubjectPosition)
return function()
local subject = workspace.CurrentCamera.CameraSubject
if
isInFirstPerson()
isInFirstPerson(workspace.CurrentCamera)
and typeof(subject) == "Instance"
and subject:IsA("Humanoid")
and subject.Health > 0
Expand Down Expand Up @@ -139,10 +134,10 @@ local function wrapSetupTransparency(setupTransparency)
end
end

local function setupSmoothRotation(config: Config)
local camera = workspace.CurrentCamera
local function setupSmoothRotation(camera: Camera, config: Config)
local humanoid: Humanoid?
local conn1 = camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()

local function setupCameraSubject()
local cameraSubject: Instance? = camera.CameraSubject
if cameraSubject and cameraSubject:IsA("Humanoid") then
humanoid = cameraSubject
Expand All @@ -156,14 +151,23 @@ local function setupSmoothRotation(config: Config)
else
humanoid = nil
end
end)
end
local conn1 =
camera:GetPropertyChangedSignal("CameraSubject"):Connect(setupCameraSubject)
setupCameraSubject()

local conn2 = UserGameSettings:GetPropertyChangedSignal("RotationType")
:Connect(function()
local rotationType = UserGameSettings.RotationType
if humanoid then
humanoid.AutoRotate = rotationType ~= Enum.RotationType.CameraRelative
end
end)
local rotationType = UserGameSettings.RotationType
if humanoid then
humanoid.AutoRotate = rotationType ~= Enum.RotationType.CameraRelative
end

RunService:BindToRenderStep("FirstPersonCamera", 1000, function(delta: number)
local rotationType = UserGameSettings.RotationType
if
Expand Down Expand Up @@ -224,7 +228,7 @@ local function setupSmoothRotation(config: Config)
end
end

if isInFirstPerson() then
if isInFirstPerson(camera) then
local cf = camera.CFrame
local headPos = getSubjectPosition()
if headPos then
Expand All @@ -237,6 +241,9 @@ local function setupSmoothRotation(config: Config)
end
end)
return function()
if humanoid then
humanoid.AutoRotate = true
end
conn1:Disconnect()
conn2:Disconnect()
RunService:UnbindFromRenderStep("FirstPersonCamera")
Expand All @@ -245,8 +252,6 @@ end

local FirstPersonCamera = {}

FirstPersonCamera.isInFirstPerson = isInFirstPerson

-- Called once to start the FirstPersonCamera logic.
-- Binds and overloads everything necessary.
export type Config = {
Expand Down Expand Up @@ -285,7 +290,6 @@ function FirstPersonCamera.start(config: Config)
local baseTransparencyControllerSetupTransparency =
transparencyController.SetupTransparency

-- local rotListener = UserGameSettings:GetPropertyChangedSignal("RotationType")
local cleanupSmoothRotation
local function setSmoothRotation(new, old)
if new and not old then
Expand All @@ -294,14 +298,9 @@ function FirstPersonCamera.start(config: Config)
return baseBaseCameraGetSubjectPosition(baseCamera)
end)
baseCamera.GetSubjectPosition = getSubjectPosition
cleanupSmoothRotation = setupSmoothRotation(config)
cleanupSmoothRotation = setupSmoothRotation(workspace.CurrentCamera, config)
elseif old and not new then
cleanupSmoothRotation()
local camera = workspace.CurrentCamera
local humanoid = camera.CameraSubject
if humanoid then
humanoid.AutoRotate = true
end
baseCamera.GetSubjectPosition = baseBaseCameraGetSubjectPosition
end
end
Expand Down
10 changes: 6 additions & 4 deletions client/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ type Rotator = {

local rotators: { [Model]: Rotator } = {}

local function isInFirstPerson()
return FirstPersonCamera.isInFirstPerson()
local function isInFirstPerson(camera: Camera)
return if camera.CameraType ~= Enum.CameraType.Scriptable
then (camera.Focus.Position - camera.CFrame.Position).Magnitude <= 1
else false
end

local function round(number: number, factor: number)
Expand Down Expand Up @@ -87,7 +89,7 @@ end
-- If no lookVector is provided, the camera's lookVector is used instead.
-- useDir (-1 or 1) can be given to force whether the direction is flipped or not.
local function computeLookAngle(config: Config)
local inFirstPerson = isInFirstPerson()
local inFirstPerson = isInFirstPerson(workspace.CurrentCamera)
local yaw, pitch, dir = 0, 0, 1

local lookVector = workspace.CurrentCamera.CFrame.LookVector
Expand Down Expand Up @@ -215,7 +217,7 @@ local function updateCharacter(delta, config: Config, character, rotator: Rotato
local yaw = yawState.Current

if character == Players.LocalPlayer.Character and name == "Head" then
if isInFirstPerson() then
if isInFirstPerson(workspace.CurrentCamera) then
pitch = pitchState.Goal
yaw = yawState.Goal
end
Expand Down
2 changes: 1 addition & 1 deletion client/wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fewkz/character-realism-client"
version = "0.2.0"
version = "0.2.1"
license = "MPL-2.0"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
Expand Down

0 comments on commit eaabb74

Please sign in to comment.