Hello i am making a 2D game .
And i want to force player face right and left direction
like if he press A I want to make player face Left like
And if he press D then i want to make him face Right like this
Hello i am making a 2D game .
And i want to force player face right and left direction
like if he press A I want to make player face Left like
And if he press D then i want to make him face Right like this
First you need a direction vector which can be achieved with:
--Player position being the character's primary part
local Direction = (PlayerPosition - GoalPosition).Unit * 1000
--The goal position should be a direction you want the player to face for example it could be:
local GoalPosition = PlayerPosition + Vector3.new(0, 0, 3)
After you have your direction you just need to face the player as such which can be done either with a BodyGyro, AlignOrientation or just by setting the character CFrame
Setting the CFrame can be slightly snappy so if it is so just use a AlignOrientation but I will code up an example for the CFrame:
local GoalCFrame = CFrame.new(Character:GetPrimaryPartCFrame().Position), Direction)
--CFrame.new constructor will calculate for you to look at a direction when you include 2 arguments
Character:SetPrimaryPartCFrame(GoalCFrame)
First of all you can disable roblox default controls
local p = game.Players.LocalPlayer
local PlayerModule = require(p.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
Controls:Disable()
Then you can programm your own movements using the UserInputService and Humanoid:Move()
An example would be:
--Disable roblox control
local p = game.Players.LocalPlayer
local PlayerModule = require(p.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
Controls:Disable()
function refreshDirection()
--Check if the character is alive
local character = p.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
--Set the moving direction
local direction = Vector3.new((keyD and 1 or 0)+(keyA and -1 or 0))
humanoid:Move(direction)
end
end
end
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
--When a key is pressed
local key = input.KeyCode
if key==Enum.KeyCode.D then
keyD = true
refreshDirection()
elseif key==Enum.KeyCode.A then
keyA = true
refreshDirection()
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
--When a key is released
local key = input.KeyCode
if key==Enum.KeyCode.D then
keyD = false
refreshDirection()
elseif key==Enum.KeyCode.A then
keyA = false
refreshDirection()
end
end
end)
Hope it helps.
Thanks guys for ur help . But i have got some work to do I will check if it works or not tomorrow
This line is giving an error
local GoalCFrame = CFrame.new(Character:GetPrimaryPartCFrame().Position), Direction)
Error:
Workspace.RACHITMAGAR0.LocalScript:17: Expected identifier when parsing expression, got ')