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

Is there a way I can limit my CFrame code to make the character unable to look all the way back?

Asked by 3 years ago

Here's a demonstration of what I mean. https://gyazo.com/57250a79a320571f62150b4ea4c10c06

The character isn't supposed to look back like this when the camera goes down on the gif. The character should keep on looking forward to the mouse.

It might be more apparent with my code and well obviously here it is:

local tool = script.Parent
local camera = game:GetService("Workspace").CurrentCamera
local runService = game:GetService("RunService")
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local toolActivated = false
local mouse = plr:GetMouse()
local UserInputService = game:GetService("UserInputService")
local BodyGyro = Instance.new("BodyGyro")
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild("Humanoid")

BodyGyro.MaxTorque = Vector3.new(100000000, 100000000, 100000000)
BodyGyro.D = 200
BodyGyro.P = 12000


tool.Activated:Connect(function()
    toolActivated = true
end)


tool.Deactivated:Connect(function()
    toolActivated = false
    BodyGyro.Parent = nil
end)

runService.RenderStepped:Connect(function()
    if humanoid.Health > 0 and toolActivated then
        BodyGyro.Parent = HumanoidRootPart
        local HRPpos = HumanoidRootPart.Position
        BodyGyro.CFrame = CFrame.new(HRPpos, Vector3.new(mouse.Hit.Position.x, HRPpos.y, mouse.Hit.Position.z)) -- Make the HumanoidRootPart face the direction of the mouse cursor through a BodyGyro.
    end
end)

humanoid.Died:Connect(function()
    BodyGyro:Destroy()
end)

As you can see, I have a CFrame.new coming from the BodyGyro.Parent part. I realise that I need to add something to the CFrame.new part so that this does not occur, but I don't know what I have to add there.

Any idea what I must put there?

Answer this question