How to make the character face your Controllers Joystick?

I am looking for a way to make it so your character can face your XBOX 360’s Joystick, like how you can do with a normal mouse.

I am pretty new to Xbox controller support, and i only know how to input buttons and such.

This is my mouse script:

game:GetService(“RunService”).RenderStepped:Connect(function()
c.Humanoid.AutoRotate = false
c.Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
c.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
cam.CameraType = Enum.CameraType.Scriptable
local pos = CFrame.new(c.Head.Position.X,10,c.Head.Position.Z) + Vector3.new(0,20,10)
cam:Interpolate(pos,c.Head.CFrame,.1)
c.HumanoidRootPart.CFrame = c.HumanoidRootPart.CFrame:lerp(CFrame.new(c.HumanoidRootPart.Position, Vector3.new(m.Hit.p.x, c.HumanoidRootPart.Position.Y, m.Hit.p.z)), .25)
end)

Id accept anything really. Even a wiki link would help.

Thanks

2 Likes

http://wiki.roblox.com/index.php?title=API:Class/Humanoid/MoveDirection

This returns the current direction of movement, you can set the HumanoidRootPart CFrame’s lookVector to this (with an exception case for when MoveDirection’s magnitude is 0)

1 Like

Would this work, if say i wanted my character to move forward, but he can also face backwards while running?

Yeah, you could set it to negative MoveDirection.

You can use the CFrame.new(Vector3 pos, Vector3 lookAt) constructor to make the HumanoidRootPart face a direction, which in this case is the (right) thumbstick position.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputChanged:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Thumbstick2 then
		local position = input.Position
		
		-- If the stick goes 10% past the center, activate
		if position.Magnitude > 0.1 then
			Root.CFrame = CFrame.new(
				Root.Position,
				Root.Position + Vector3.new(position.X, 0, -position.Y)
			)
		end
	end
end)

If you want to rotate it relative to the camera the solution is a bit more complex, but still similar:

local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")

local Camera = Workspace.CurrentCamera

local function Rotate(vector, angle)
	return Vector2.new(
		vector.X * math.cos(angle) + vector.Y * math.sin(angle),
		-vector.X * math.sin(angle) + vector.Y * math.cos(angle)
	)
end

UserInputService.InputChanged:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Thumbstick2 then
		local position = input.Position
		
		if position.Magnitude > 0.1 then
			local look = Camera.CFrame.LookVector
			local angle = math.atan2(look.Z, look.X) + math.pi / 2
			
			position = Rotate(position.Unit, angle)
			
			Root.CFrame = CFrame.new(
				Root.Position,
				Root.Position + Vector3.new(position.X, 0, -position.Y)
			)
		end
	end
end)

Note: Both of these examples assume “Root” is the HumanoidRootPart

4 Likes