Detecting if user is moving sideways relative to my camera

before u link another post or telling me to use the searchbar, those are all not accurate / what im looking for

basically i just wanna see how i can detect if a user is moving sideways from my camera

(not using any kind of input from other player)

1 Like

Well I think you could do something with the lookVector of the user’s camera and the MoveDirection of the characters humanoid.

both aren’t relative to my camera though, thats in world space

1 Like

Get the Camera.CFrame.RightVector then compare it to Humanoid.MoveDirection.

could u show a code sample i dont know what u mean

This will make the move direction relative to the cam. Then you can look at the X coordinate:

local x = Camera.CFrame:VectorToObjectSpace(Humanoid.MoveDirection).X

Or this does the same thing:

local x = Humanoid.MoveDirection:Dot(Camera.CFrame.RightVector)

Could use the root part AssemblyLinearVelocity instead of MoveDirection if you wanted.

2 Likes

alright so this code works but i have another question

how would i fix this code to make it work for all sides of my camera like look
in the beginning where the user is facing me thats the effect i want, but when I turn around, or use third person the lookvectors are off

https://gyazo.com/208bf3106c1b765becc0117d307b21d0

local Part = Instance.new("Part", workspace)
Part.Anchored = true 
Part.CanCollide = false

local OtherPlayer = function()
	return game:GetService("Players"):GetPlayers()[2]
end


game:GetService("RunService").RenderStepped:Connect(function()
	local g = OtherPlayer() and OtherPlayer().Character 
	local h = g and g:FindFirstChild("Humanoid")
	local x = h and workspace.CurrentCamera.CFrame:VectorToObjectSpace(h.MoveDirection).X
	
	if x < 0 then
		Part.Position = g.PrimaryPart.Position + (g.PrimaryPart.CFrame.RightVector * Vector3.new(5, 5, 5))
	elseif x > 0 then
		Part.Position = g.PrimaryPart.Position + (-g.PrimaryPart.CFrame.RightVector * Vector3.new(5, 5, 5))
	end
end)

Sounds like you want the direction relative to the character, not the camera.

Could you describe what you’re planning to use this for? That would help me figure out what you’re actually asking.

im trying to make a projectile hit the side of the character its moving in, it works for everything except when the user is moving left, or right. like when i mean it doesnt work i just want it to be a little offsetted to the side of the direction the character is moving in

You’re saying you want to fire a projectile in a straight line from some other position, but lead the shot (i.e. fire ahead of their position based on their movement) so that it intercepts a moving target?

Yea thats what I mean, all I need is a fix for the lookvector problem I sent above, the projectile formula is 100% fine.

Try using your character’s RightVector instead of the other player.

wow that actually worked, thanks so much both of u

ok this is actually backwards if my cameras the other way since thats the world position im assuming, so any other ideas?

bumppppppppppppppppppppppppppppppppppppppppppppppppppp i j mm

Why not just add the move direction directly, in world space?

Part.Position = g.PrimaryPart.Position + g.MoveDirection.Unit * 5

so this works very well idk why i forgot abt this but it will also offset it if the player is moving straight, or any other way than just left and right of course. do u have any checks i could use?

also i cant use the one above due to it returning true if the user is moving diagonal. diagonal as in the players back is facing me btw

I would have thought it wouldn’t matter if you lead the shot when they’re moving forward or back, since it would still go straight in that case. Plus it would also work on diagonals. That seems like a good thing.

But whatever, if that’s not your question that’s fine. I take back my world space suggestion then.

I’m assuming you mean “just left and right relative to the camera”.

game:GetService("RunService").RenderStepped:Connect(function()
  local p = OtherPlayer()
  if not p then return end

  local g = p.Character
  if not g then return end

  local h = g:FindFirstChild("Humanoid")
  if not h then return end

  local root = g.PrimaryPart
  if not root then return end

  local cam = workspace.CurrentCamera

  local right = cam.CFrame.RightVector

  local dir = h.MoveDirection

  local x = dir:Dot(right)
  local pos = root.Position

  -- 0.01 is the threshold for how much left/right you have to be moving for it to lead the shot:
  if math.abs(x) > 0.01 then
    pos = pos + right * math.sign(x) * 5
  end

  Part.Position = pos
end)

Pretty sure that’ll do what you’re asking, even though I’m still not quite sure why you’d want to. I haven’t tested yet.

its perfect thanks

thirtycharacters