Applying a directional vector to a movement vector

i’ve been working on doing custom movement using a controllermanager (wishing to make a quake-esque movement system) but i’m currently stuck trying to figure out how to apply the facing/directional direction to the actual movement direction

heres the full script of what i have so far (basic implementation of custom movement, ill add onto this later once i actually figure this thing out)

-- services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- important objects
local ClientPlayer = game:GetService("Players").LocalPlayer
local ClientCamera = workspace.CurrentCamera

local Character = script.Parent
local ControllerManager:ControllerManager = Character:WaitForChild("ControllerManager")

-- the main event
RunService.PreSimulation:Connect(function(dt)
	local wish_velocity:vector = vector.create(0,0,0) -- the desired movement
	
	local forward = (UserInputService:IsKeyDown(Enum.KeyCode.W) and -1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.S) and 1 or 0)
	local right = (UserInputService:IsKeyDown(Enum.KeyCode.D) and 1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0)
	wish_velocity = vector.create(right, 0, forward)
	
	local direction = ClientCamera.CFrame.RightVector:Cross(vector.create(0,1,0)) -- the direction the player/camera is facing
	
	ControllerManager.MovingDirection = wish_velocity -- this actually makes the player move, right now it just moves according to which keys are down
	ControllerManager.FacingDirection = direction -- this makes the player face in the direction the camera is facing
end)

to clarify, i’m trying to make the wish_velocity vector face the direction that the direction vector gives
any help with what calculation can actually make this possible would be greatly appreciated, thanks!

1 Like

A bit hard to understand what you want here. Is ControllerManager meant to be a ModuleScript? I don’t see a reference to a ModuleScript. Why is this: local Character = script.Parent ??

I take it you want to just face the way the keypress is moving you.. which is default unless you set strafing and have a tool hand.

2 Likes

Is ControllerManager meant to be a ModuleScript?

ControllerManager is a part of a series of new(ish) instances that allow for characters to be made without a humanoid, quickly putting a document about how it’s setup below
https://create.roblox.com/docs/physics/character-controllers

in my case, im still using a default roblox character (which is what the Character variable links to) with a humanoid but with the “EvaluateStateMachine” property off, which disables the default roblox character controller for the humanoid

:woozy_face:.. I’ve never used this yet. It’s really late at the moment. I’ll have to research a bit to get a grip on this. I’ll look at it tomorrow. I did see an NPC on the page you linked. That is usable, maybe that can help for now. That is one of Roblox’s made NPCs.. NPC kit.. It’s in the tools box, RO-01 Robots

1 Like
This works for me
--!strict

-- services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- important objects
local ClientPlayer = game:GetService("Players").LocalPlayer
local ClientCamera = workspace.CurrentCamera

local Character = script.Parent
local ControllerManager:ControllerManager = Character:WaitForChild("ControllerManager")

-- the main event
RunService.PreSimulation:Connect(function(dt)
	local forward = (UserInputService:IsKeyDown(Enum.KeyCode.W) and -1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.S) and 1 or 0)
	local right = (UserInputService:IsKeyDown(Enum.KeyCode.D) and 1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0)
	local wish_velocity = vector.create(right, 0, forward)

	local direction = ClientCamera.CFrame.LookVector

	ControllerManager.MovingDirection = ClientCamera.CFrame:VectorToWorldSpace(Vector3.new(wish_velocity.x, wish_velocity.y, wish_velocity.z))
	ControllerManager.FacingDirection = direction
end)

Please check recording of test without character (press F8):
:white_check_mark: model looks at camera direction
:white_check_mark: WASD moves model according model’s orientation

The file I used:
character_controller.rbxl (64.7 KB)

For the code to work I suspect that the detail you should be focused on is RootPart’s orientation - it must point from model’s front as well (select RootPart in explorer and right click on it in view port to access “Show/Hide Orientation Indicator” menu option):

1 Like

THIS IS REALLY GOOD ACTUALLY only thing is that looking up and down slows down the movement but i think i can fix that myself, thank you so much really :face_holding_back_tears:

Fixed
--!strict

-- services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- important objects
local ClientPlayer = game:GetService("Players").LocalPlayer
local ClientCamera = workspace.CurrentCamera

local Character = script.Parent
local ControllerManager:ControllerManager = Character:WaitForChild("ControllerManager")

-- the main event
RunService.PreSimulation:Connect(function(dt)
	local forward = (UserInputService:IsKeyDown(Enum.KeyCode.W) and -1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.S) and 1 or 0)
	local right = (UserInputService:IsKeyDown(Enum.KeyCode.D) and 1 or 0) + (UserInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0)
	local wish_velocity = Vector3.new(right, 0, forward)

	local camera_look_vector = ClientCamera.CFrame.LookVector
	local direction_cframe = CFrame.lookAt(Vector3.new(), Vector3.new(camera_look_vector.X, 0, camera_look_vector.Z))

	ControllerManager.MovingDirection = direction_cframe:VectorToWorldSpace(wish_velocity)
	ControllerManager.FacingDirection = direction_cframe.LookVector
end)
1 Like


How did a server script use .LocalPlayer here?

Good observations!
It’s a nil obviously.
And not in use. The line can be safely removed.
Use F8 to play the experience.

1 Like

Lost me, must be a !strict thing. I was wondering because I was unable to get that to do anything as far as moving and didn’t see any errors.. Guess you were showing the facing thing.

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