Hello! I’ve been making this character controller thing for my game and I’m currently working on the camera movement. All of it works except for this one part. Basically, the camera is moved forward to be infront of the torso. However, this means that the camera clips through walls and stuff. So, I have tried using a ray cast to detect walls infront of the camera and it is supposed to move the camera back when it is infront of the wall and vice versa. However, it doesn’t move at all.
It does detect the wall infront of the camera as I have tried using prints to detect when the script is working and it is detecting the wall, it just doesn’t move the camera and I don’t know why. There are also no errors at all.
What can I do?
Here is the specific part of the script that uses the ray cast)
RunService.RenderStepped:Connect(function() --Hit Wall
local ray = Ray.new(Character.Head.Position, ((Character.Head.CFrame + Character.Head.CFrame.LookVector * 2) - Character.Head.Position).Position.Unit)
local ignoreList = Character:GetChildren()
local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
if hit then
CameraPosition = Vector3.new(CurrentX, CurrentY, 0)
else
CameraPosition = Vector3.new(CurrentX, CurrentY, -0.9)
end
end)
Here is the full script)
--Variables--
local UIS = game:GetService('UserInputService')
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local TweenService = game:GetService("TweenService")
-- Tweens
local NormCam = Vector3.new(0, 0, -0.9)
local CrouchCam = Vector3.new(0, -1.5, -1.25)
local MovingNormCam = Vector3.new(0, 0, -1.05)
local RunningNormCam = Vector3.new(0, 0, -1.2)
local MovingCrouchCam = Vector3.new(0, -1.5, -1.4)
local CameraPosition = Humanoid.CameraOffset
local CurrentX = Humanoid.CameraOffset.X
local CurrentY = Humanoid.CameraOffset.Y
local CurrentZ = Humanoid.CameraOffset.Z
local CamPositionCustom = NormCam
local IntoRunTweeninfo = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local OutRunTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local CrouchTweenInfo = TweenInfo.new(0.45, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local MovementChangeTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local IdleTweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local Camera = workspace.CurrentCamera
local CrouchCamPosition = Camera.CFrame * CrouchCam
local DefaultFoV = 85
local IntroRunProperties = {FieldOfView = DefaultFoV + 4.5}
local OutOfRunProperties = {FieldOfView = DefaultFoV - 4.5}
local CrouchIntoProperties = {CameraOffset = CrouchCam}
local CrouchOutOfProperties = {CameraOffset = NormCam}
local IntoCrouchWalkProperties = {CameraOffset = MovingCrouchCam}
local IntoWalkProperties = {CameraOffset = MovingNormCam}
local IntoRunMovingProperties = {CameraOffset = RunningNormCam}
local IdleProperties = {CameraOffset = NormCam}
local RunTween = TweenService:Create(game.Workspace.CurrentCamera, IntoRunTweeninfo, IntroRunProperties)
local OutOfRunTween = TweenService:Create(game.Workspace.CurrentCamera, OutRunTweenInfo, OutOfRunProperties)
local IntoCrouchWalkTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoCrouchWalkProperties)
local IntoWalkTweenTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoWalkProperties)
local IntoRunMovingTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoRunMovingProperties)
local IdleTween = TweenService:Create(Humanoid, IdleTweenInfo, IdleProperties)
local IntoCrouchTween = TweenService:Create(Humanoid, CrouchTweenInfo, CrouchIntoProperties)
local OutCrouchTween = TweenService:Create(Humanoid, CrouchTweenInfo, CrouchOutOfProperties)
-- UI --
local Gui = Player.PlayerGui:WaitForChild("MobileUI"):WaitForChild("Screen")
local GuiCrouch = Gui:WaitForChild("CrouchButton")
local GuiRun = Gui:WaitForChild("RunButton")
local UIS = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local PlayerLight = ReplicatedStorage:WaitForChild("AmbienceLight")
local PlayerlightClone = PlayerLight:Clone()
PlayerlightClone.Parent = Character:WaitForChild("Torso")
local Flashlight = Character:WaitForChild("Flashlight")
local LightEmitter = Flashlight:WaitForChild("FlashlightLightEmitter")
local Rings = Flashlight:WaitForChild("FlashlightRings")
local RunKey = Enum.KeyCode.LeftShift or Enum.KeyCode.ButtonL2
local CrouchKey = Enum.KeyCode.C or Enum.KeyCode.ButtonB
local Running = false
local Crouching = false
local HumanoidMoving = false
local StaminaAmount = 150
StaminaAmount = math.clamp(StaminaAmount, 0, 150)
-- Functions --
local function RunKeyPressed()
if StaminaAmount > 0 then
Running = true
RunTween:Play()
wait(0.45)
CameraPosition = NormCam
CamPositionCustom = NormCam
while StaminaAmount > 0 and Running == true do -- Stamina decrease
StaminaAmount = StaminaAmount - 1
wait(0.1)
if StaminaAmount <= 0 then -- Ran out of Stamina
OutOfRunTween:Play()
CameraPosition = NormCam
CamPositionCustom = NormCam
Running = false
end
end
end
end
local function CrouchKeyPressed()
Crouching = true
Running = false
IntoCrouchTween:Play()
wait(0.45)
CameraPosition = CrouchCam
CamPositionCustom = CrouchCam
end
local function RunKeyReleased()
Running = false
OutOfRunTween:Play()
CameraPosition = NormCam
CamPositionCustom = NormCam
while StaminaAmount < 150 and Running == false do
StaminaAmount = StaminaAmount + 0.5
wait(0.1)
if StaminaAmount <= 0 then
Running = false
CameraPosition = NormCam
CamPositionCustom = NormCam
end
end
end
local function CrouchKeyReleased()
Crouching = false
OutCrouchTween:Play()
wait(0.45)
CameraPosition = NormCam
CamPositionCustom = NormCam
end
-- Dececting input --
UIS.InputBegan:Connect(function(input)
if input.KeyCode == RunKey then
RunKeyPressed()
elseif input.KeyCode == CrouchKey then
CrouchKeyPressed()
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == RunKey then
RunKeyReleased()
elseif input.KeyCode == CrouchKey then
CrouchKeyReleased()
end
end)
GuiRun.MouseButton1Down:Connect(RunKeyPressed)
GuiCrouch.MouseButton1Down:Connect(CrouchKeyPressed)
GuiRun.MouseButton1Up:Connect(RunKeyReleased)
GuiCrouch.MouseButton1Up:Connect(CrouchKeyReleased)
--Change Transparency--
for childIndex, child in pairs(Character:GetChildren()) do
if child:IsA("BasePart") and child.Name ~= "Head" then
child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
child.LocalTransparencyModifier = child.Transparency
LightEmitter.LocalTransparencyModifier = LightEmitter.Transparency
Rings.LocalTransparencyModifier = Rings.Transparency
end)
end
end
--Detecting Movement--
local function DetectMovement()
if Humanoid.MoveDirection.Magnitude > 0 then
HumanoidMoving = true
if Running == true then -- Player is running
IntoRunMovingTween:Play()
elseif Crouching == true then -- Player is crouching
IntoCrouchWalkTween:Play()
elseif Running == false and Crouching == false then -- Player is walking
IntoWalkTweenTween:Play()
end
elseif Humanoid.MoveDirection.Magnitude == 0 then -- Player is not moving
if Crouching == true then
IntoCrouchTween:Play()
else
IdleTween:Play()
end
end
end
RunService.Heartbeat:Connect(DetectMovement)
--If the player steps in a vehicle--
Camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
if Camera.CameraSubject:IsA("VehicleSeat") then
Camera.CameraSubject = Humanoid
end
end)
RunService.RenderStepped:Connect(function() --Hit Wall
local ray = Ray.new(Character.Head.Position, ((Character.Head.CFrame + Character.Head.CFrame.LookVector * 2) - Character.Head.Position).Position.Unit)
local ignoreList = Character:GetChildren()
local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
if hit then
CameraPosition = Vector3.new(CurrentX, CurrentY, 0)
else
CameraPosition = Vector3.new(CurrentX, CurrentY, -0.9)
end
end)
Thank you for any help!!