Why isn't this code working?

Right now all I need is the formula, but I don’t like using things I don’t understand.

Yeah, what you are trying to create is really hard, I think even the most expert programmers would need a week or 2 to figure this out :sweat_smile:

did you set the camera type to be scriptable so Enum.CameraType.Scriptable

I’m not trying to change the position of the camera, I’m trying to rotate the movedirection vector, so that it’s axises are aligned with the camera axis.

still needs to be scriptable because you are still using a script to modify something of it

I’m not modifying the camera angle at all. Just reading it’s CFrame. That’s all I need from it. It doesn’t have to be scriptable.

just try it atleast because you can always undo it

Okay, I got something that’ll do what you want it to but only in the forwards direction. It doesn’t change the rotation of the humanoidrootpart and sometimes just outright breaks, but I guess it’s something so I’ll leave it here in case nobody else answers.

local RunService = game:GetService("RunService");

local Players = game:GetService("Players");
local LocalPlayer = Players.LocalPlayer;
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait();
local UserInputService = game:GetService("UserInputService")
local ForwardAttachment = Instance.new("Attachment");
local BodyMover = Instance.new("VectorForce");
ForwardAttachment.CFrame = Character:WaitForChild("HumanoidRootPart").CFrame;
BodyMover.Parent = Character:WaitForChild("HumanoidRootPart");
BodyMover.Attachment0 = ForwardAttachment;
BodyMover.ApplyAtCenterOfMass = true;
BodyMover.Enabled = false;

local Camera = workspace.CurrentCamera;
ForwardAttachment.Parent = Character:WaitForChild("Head");

RunService.RenderStepped:Connect(function()
	
	local MoveDirection = (Camera.CFrame:ToObjectSpace(Character.HumanoidRootPart.CFrame)):Inverse().LookVector.Unit*9999
	MoveDirection = Vector3.new(MoveDirection.X,MoveDirection.Y, MoveDirection.Z);
	BodyMover.Force = MoveDirection;
	
end)

UserInputService.InputBegan:Connect(function(k,gpe)
	if not gpe and k.KeyCode == Enum.KeyCode.W then
		BodyMover.Enabled = true;
	end
end)

UserInputService.InputEnded:Connect(function(k,gpe)
	if not gpe and k.KeyCode == Enum.KeyCode.W then
		BodyMover.Enabled = false;
	end
end)

Yessir. You want me to make the camera scriptable?

ye just incase because it can be picky with enums

camera.CFrame:VectorToWorldSpace(hum.MoveDirection) takes the humanoid’s movedirection and treats it as if that move direction was supposed to be relative to the camera’s cframe. then it returns the corresponding vector with respect to the workspace cframe

1 Like

But would it “add” the movedirection in relation to the camera orientation? If the movedirection was forward, or negative z, and the camera was looking up, or it’s lookvector was 0, 1, 0, the movedirection would be 0, 1, 0, right?

if move direction is (0,0,-1) which is moving forward, and the camera is pointing toward (0,1,0), then this will return (0,1,0)

1 Like

This is exactly what I need. But it’s not working for me. I’m setting the force to that direction, but it’s all warped and twisted.

I can’t set the camera to scriptable or else it won’t move, and I need it to move and follow the player for what I’m doing

1 Like

because you are referencing the current move direction to set a force to change the move direction. thats gonna cause a feedback loop which will glitch everything out lol.

you should use the user input for where they want to move rather than use the constantly changing humanoid move direction.

1 Like

Ah, I see what you’re saying. The reason why I did this originally was because I didn’t want to have to add a whole bunch of compatibility for different input types and devices. Is there anyway I can kinda “undo” the movedirection? Because clearly if you press w it’s not always going to be pointing in the direction of 0, 0, -1.

it seems you can get the user input move direction from the player module script. however last time i tried (maybe a year ago) they intentionally prevent you from accessing this script. try anyways

local Controller = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls() 

local forceVector = Vector3.new()

function setForceVector() 
    forceVector = camera.CFrame:VectorToWorldSpace(Controller:GetMoveVector())
end

something like this

1 Like

Ah Yes! This! I used this before, I totally forgot about this thing. Thank you, let me see if it works.

1 Like

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