I made a script that when you hold the E key down I want to make it be aim’able via the mouse (like the root part rotates toward the mouse) but the obvious solution (Cframe the rootpart to mouse.hit)
makes it look glitchy? so how do I make it looks sophisticated??
UIP.InputBegan:Connect(function(Input, GPE)
if GPE then return end
if Input.KeyCode == Enum.KeyCode.E and Debounce == 1 and toolquip == true then
print("Local Script Recieved Input . . .")
Debounce = 2
spawn(function()
ic = true
wait(0.41)
ic = false
end)
track:Play()
wait(0.15)
track:AdjustSpeed(0)
holdconnecting:FireServer(player)
end
end)
local UserInputService = game:GetService("UserInputService")
local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Hum = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local IsRotating = false
------------------------------------------------------
local function StartRotation()
Hum.AutoRotate = false
task.spawn(function()
repeat
Root.CFrame = CFrame.lookAt(Root.Position, Vector3.new(Mouse.Hit.Position.X, Root.Position.Y, Mouse.Hit.Position.Z))
task.wait()
until
IsRotating == false
end)
Hum.AutoRotate = true
end
local function RotationInput(Input, Processing)
if Input.KeyCode == Enum.KeyCode.E and not Processing and IsRotating == false then
IsRotating = true
StartRotation()
elseif Input.KeyCode == Enum.KeyCode.E and not Processing and IsRotating == true then
IsRotating = false
end
end
------------------------------------------------------
UserInputService.InputBegan:Connect(RotationInput)
UserInputService.InputEnded:Connect(RotationInput)
First, make sure you’re using CFrame. I would use the second CFrame.new parameter, in which you give 2 positions, the first the position of the cframe, and the second the position it’s looking at, but I would modify the Y of the second to match the HumanoidRootParts. That second parameter could be your mouse.
thank you all for your kind attempts, however I regret to inform I explained that cframing the root to the mouse is not what I need, as if we hover hover a object we get jittery/spaztic movements
my goal is to get something smooth where its not detecting the mouses hit position but more like the mouses general direction?
you can achieve something like this using AlignOrientation
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local playerCharacter = player.Character or player.CharacterAdded:Wait()
local playerRootPart = playerCharacter:FindFirstChild("HumanoidRootPart")
local playerHumanoid = playerCharacter:FindFirstChild("Humanoid")
-- Disabled these states to prevent humanoid flinging when near objects.
playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
local function setupRotation(playerRootPart: BasePart)
local createdAttachment = Instance.new("Attachment")
createdAttachment.Name = "RotateAttachment"
createdAttachment.Parent = playerRootPart
local createdAlignRotation = Instance.new("AlignOrientation")
createdAlignRotation.Mode = Enum.OrientationAlignmentMode.OneAttachment
createdAlignRotation.Attachment0 = createdAttachment
createdAlignRotation.RigidityEnabled = true
createdAlignRotation.CFrame = playerRootPart.CFrame
createdAlignRotation.Parent = playerRootPart
return createdAlignRotation, createdAttachment
end
local updateConnection, orientationInstance, orientationAttachment = nil, nil, nil
local LOOK_FACTOR = 1 -- higher the number, the more accurate the rotation will be towards the mouse hit position.
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then
return
end
if input.KeyCode ~= Enum.KeyCode.E then
return
end
if updateConnection or orientationInstance then
return
end
orientationInstance, orientationAttachment = setupRotation(playerRootPart)
updateConnection = RunService.RenderStepped:Connect(function()
local directionVector = (playerRootPart.Position - mouse.Hit.Position)
local orientation = CFrame.lookAt(playerRootPart.Position, -directionVector * (directionVector.Magnitude * LOOK_FACTOR))
orientationInstance.CFrame = orientation
end)
end)
UserInputService.InputEnded:Connect(function(input: InputObject)
if input.KeyCode ~= Enum.KeyCode.E then
return
end
if not updateConnection or not orientationInstance then
return
end
updateConnection:Disconnect()
orientationAttachment:Destroy()
orientationInstance:Destroy()
orientationInstance = nil
updateConnection = nil
end)
local tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local Remote = tool:WaitForChild("RemoteEvent")
local Debounce = 1
local anim = script.Animation
local track = char.Humanoid:LoadAnimation(anim)
local ic = script.InstaCast
local toolquip = false
local mouse = player:GetMouse()
local holdconnecting = tool:WaitForChild("HoldConnecting")
local UIP = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
print("Variables_Loaded")
local playerHumanoid = char:FindFirstChild("Humanoid")
-- Disabled these states to prevent humanoid flinging when near objects.
playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
local function setupRotation(playerRootPart: BasePart)
local createdAttachment = Instance.new("Attachment")
createdAttachment.Name = "RotateAttachment"
createdAttachment.Parent = hrp
local createdAlignRotation = Instance.new("AlignOrientation")
createdAlignRotation.Mode = Enum.OrientationAlignmentMode.OneAttachment
createdAlignRotation.Attachment0 = createdAttachment
createdAlignRotation.RigidityEnabled = true
createdAlignRotation.CFrame = hrp.CFrame
createdAlignRotation.Parent = hrp
return createdAlignRotation, createdAttachment
end
local updateConnection, orientationInstance, orientationAttachment = nil, nil, nil
local LOOK_FACTOR = 0.1 -- higher the number, the more accurate the rotation will be towards the mouse hit position.
local IsRotating = false
local function Equipped()
print(". . . Detected Equip")
toolquip = true
end
local function UnEquipped()
print(" . . . Detected Unequip")
toolquip = false
end
print("Functions_Loaded")
tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(UnEquipped)
UIP.InputBegan:Connect(function(Input, GPE)
if GPE then return end
if Input.KeyCode == Enum.KeyCode.E and Debounce == 1 and toolquip == true then
print("Local Script Recieved Input . . .")
Debounce = 2
holdconnecting:FireServer(player)
spawn(function()
ic = true
wait(0.41)
ic = false
print("Insta_Cast_not")
end)
if updateConnection or orientationInstance then
return
end
orientationInstance, orientationAttachment = setupRotation(hrp)
updateConnection = RunService.RenderStepped:Connect(function()
local directionVector = (hrp.Position - mouse.Hit.Position)
local orientation = CFrame.lookAt(hrp.Position, -directionVector * (directionVector.Magnitude * LOOK_FACTOR))
orientationInstance.CFrame = orientation
end)
track:Play()
wait(0.15)
track:AdjustSpeed(0)
print("Input_Began_Loaded")
end
end)
UIP.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.E and Debounce == 2 then
repeat wait() until ic == false
print(". . . Sending To Server")
track:AdjustSpeed(1)
Debounce = 3
Remote:FireServer(char, player, UIP, tool)
print(". . . SENT . . .")
if not updateConnection or not orientationInstance then
return
end
updateConnection:Disconnect()
orientationAttachment:Destroy()
orientationInstance:Destroy()
orientationInstance = nil
updateConnection = nil
wait(4) -- cooldown
Debounce = 1
print("Local_Script_Ended")
end
end)