How to Make VR Movement Relative to where User is Facing

,

So I’ve been trying for the past week to get a joystick vr movement script that moves relative to where the player is facing. So if they press the joystick forward while facing forward they go forward and if facing backwards they press on the joystick forward they would go backwards. I figured it needed to have lookvector incorporated but I could not for the life of me get it to work

This is my code for updating the vr player’s position in the game world:

local function UpdateOffset(input,processed) --Gets Called every time input is detected from vr device
	if input.UserInputType == Enum.UserInputType.Gamepad1 and input.UserInputState == Enum.UserInputState.Change or input.UserInputState == Enum.UserInputState.Begin then -- Stick 1
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			local HeadPos = UserInputService:GetUserCFrame(VRService.GuiInputUserCFrame)
			
            -- \/ \/ \/ Gameworld offset (What I'm Having Problems With) \/ \/ \/
			local off = Vector3.new(input.Position.Y*HeadPos.LookVector.X,0,input.Position.Y*HeadPos.LookVector.Z) + Vector3.new(input.Position.X*HeadPos.LookVector.Z,0,input.Position.X*HeadPos.LookVector.X)
			
			--print(off)
			
			gameworldOffset = gameworldOffset + off -- Actual gameworld pos
		end
	end
end

The function that updates the actual parts does work completely fine (Not shown in the code).

Any help would be appreciated, thanks! :smiley:

3 Likes

I have never used VR, but you might be able to use

CFrame.LookVector()

Yes I’ve tried to use LookVector by multiplying it by the input.Position.X and input.Position.Y but it doesn’t seem to completely work

Try this: (Again I’ve never used VR)

local function UpdateOffset(input,processed) --Gets Called every time input is detected from vr device
	if input.UserInputType == Enum.UserInputType.Gamepad1 and input.UserInputState == Enum.UserInputState.Change or input.UserInputState == Enum.UserInputState.Begin then 
        if input.KeyCode == Enum.KeyCode.Thumbstick1 then
              local Head = Player.Character.Head
              Player.Character:SetPrimaryPartCFrame(Head.CFrame.LookVector * 5 --Move the player Character 5 studs forward
		end
	end
end

While this does work It isn’t what I’m looking for

This code only moves the player forward in the direction it is looking at. I would like to have the direction that the lookvector is in directly affecting the joystick movement (For example on computer, setting camera a certain way and w,a,s,d still work but just in directions relative to the camera position)

Thanks for the suggestion tho :smiley:

When the player moves there head, the characters head in-game rotates accordingly so you will move where you face

Yes but what I’m saying is that when joystick movement is changed the character will ONLY move forward. I want that forward direction but WITH joystick movements incorporated. So being able to go forward, left, and right with the lookvector. Not just straight foward.

Update: Still having problems on getting this working, again if anyone has any solutions please respond asap. Thanks!

Again I still have no solution and have not gotten any closer, if anyone knows about how to make this joystick movement math work please tell me. Thanks! (I may try to look through how the Xbox movement script works to see if I can get anything out of that [I’ll update if I find something that works[)

I ended up fixing it (I totally forgot about RightVector XD)

This is the code I ended up using if anyone comes across this thread again

local Sensitivity = 4 --Divides how quick movement is

local HeadPos = UserInputService:GetUserCFrame(Enum.UserCFrame.Head)
			
local offthing = input.Position.Y*HeadPos.LookVector + input.Position.X*HeadPos.RightVector
local off = Vector3.new(offthing.X,0,offthing.Z)/Sensitivity
gameworldOffset = gameworldOffset + off
4 Likes

Cframe value for where the VR player’s root part is (Not relative to room)

Here is my module script that has the movement in it (It’s not well documented but it has most of the code there) Feel free to use any of it :slight_smile:

VRMovement.lua (4.5 KB)

1 Like

@gameknight9992005, is your CameraType different and such?

1 Like

I am just using the default camera type (Fixed I think) and the update offset function is called with UIS.InputChanged because the function sorts through all the user’s inputs (For VR players)

Example:

UserInputService.InputChanged:Connect(UpdateOffset)

Also, the update offset function is just the function that takes user input and edits a Vector3 value.
Btw the UpdateCFrames function should also be called every frame to get an immediate response to changing their joystick direction. :smiley:

(All of my scripts are local except for some of the replication scripts on server side [This means that all of the vr players body parts will have to be networked owned by themselves in order to properly replicate])

1 Like

Thank you for your help and time.

1 Like

No problem :smiley:

Feel free to dm me if you have any more questions!

1 Like