How to snap a character to a certain direction

Hi! So I’m making an undertale-like game and to keep the theme, I need to make the character’s direction snap to when the player press the movement keys. Any help will be appreciated!

You would need to detect that input they are doing, and if you are using CFrames, you can either rotate it using math.pi/2, or math.rad(90), which should rotate them a 90 degree angle.

Your best tools in this situation are going to be CFrame.UpVector, CFrame.RightVector, and CFrame.LookVector

Try something like this:

local dir = 0
local char = game:GetService("Players").LocalPlayer.Character
local moving = false

if dir == 0 then --Down
   local root = char:FindFirstChildWhichIsA("Humanoid").RootPart
   root.Anchored = true
   local torso = char:FindFirstChild("Torso")
   if root ~= nil and torso ~= nil then
      local posChange = Vector3.new(0, 0, 0)
      if moving == true then
         posChange = -root.CFrame.UpVector * 2
      end
      torso.CFrame = CFrame.new(root.CFrame.Position + posChange, root.CFrame.Position + posChange - root.CFrame.UpVector
   end
end
if dir == 1 then --Right
   local root = char:FindFirstChildWhichIsA("Humanoid").RootPart
   root.Anchored = true
   local torso = char:FindFirstChild("Torso")
   if root ~= nil and torso ~= nil then
      local posChange = Vector3.new(0, 0, 0)
      if moving == true then
         posChange = root.CFrame.RightVector * 2
      end
      torso.CFrame = CFrame.new(root.CFrame.Position + posChange, root.CFrame.Position + posChange + root.CFrame.RightVector
   end
end
if dir == 2 then --Up
   local root = char:FindFirstChildWhichIsA("Humanoid").RootPart
   root.Anchored = true
   local torso = char:FindFirstChild("Torso")
   if root ~= nil and torso ~= nil then
      local posChange = Vector3.new(0, 0, 0)
      if moving == true then
         posChange = root.CFrame.UpVector * 2
      end
      torso.CFrame = CFrame.new(root.CFrame.Position + posChange, root.CFrame.Position + posChange + root.CFrame.UpVector
   end
end
if dir == 3 then --Left
   local root = char:FindFirstChildWhichIsA("Humanoid").RootPart
   root.Anchored = true
   local torso = char:FindFirstChild("Torso")
   if root ~= nil and torso ~= nil then
      local posChange = Vector3.new(0, 0, 0)
      if moving == true then
         posChange = -root.CFrame.RightVector * 2
      end
      torso.CFrame = CFrame.new(root.CFrame.Position + posChange, root.CFrame.Position + posChange - root.CFrame.RightVector
   end
end

the way you are checking if something is a value, its very inefficient.

Also please do not write entire scripts.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.