How do i remove the Y vector from CFrame.LookVector?

am trying to make custom character movement using lookvector for my game, and it works fine, however, i found out that when i look straight in a direction and move, it moves normally, but when i look down in that direction and move, the distance i move is so low.
basically this is what i want
Untitled

the black arrow is the direction am looking at
the red dot is what lookvector returns, and the blue one is what i want to achieve

2 Likes

You can make something like this:

local look = CFrame.LookVector
look = Vector3.new(look.X,0,lookZ)

1 Like

I think you could just cross the RightVector and global “up” vector? This is assuming the CFrame you’re working with isn’t tilting to the side. I can’t get to Studio right now, so can’t test it.

local lookVector = yourCframe.RightVector:Cross(Vector3.new(0, 1, 0))

If that works, but backwards, just add a negative sign to the RightVector -yourCFrame.RightVector

6 Likes

Thanks!
i still don’t understand what the code you sent me does, but now it works as i wanted it to

2 Likes

Sorry I should have explained it! A cross product gives you the vector that is perpendicular to the two other vectors. In your case, you want to move forward, regardless of the Y tilt of the camera I assume. So a hack to get around that is to find the perpendicular vector from the right-facing look vector, which doesn’t rotate when you tilt the camera (it’s more-so the pivot point for the rotation), and the global up vector (0, 1, 0). The perpendicular vector to these two is the forward-facing vector without any tilt.

The best way to visualize this is using your hand (see Cross product - Wikipedia).

I’m glad it worked! I’ve actually needed to do this a lot in the past, and have used much more hacky solutions to do it. But I’ll be using this going forward too.

7 Likes

Thanks, I don’t personally understand it as of now, I can visualize it but I feel like Vector3.new(camera.CFrame.X, 0, camera.CFrame.Z) should have worked but idk I’m not good with math so oof hard for me to understand but thankyou.

1 Like

lets say you wanna shoot something in the direction of the camera, that thing is going to move 100 studs, so it would be (the camera’s angle)*100 right ? well the problem is, you want this thing to shoot horizontally so you only need the x and z, but if you to (x, 0, z)*100, its not gonna move 100 studs because you didn’t include the y value which is missing for it to be 100 studs

here is a drawing that explains it better

now the player will be in the middle of this, the red dot is where he is looking at, now since as i said, i want to shoot the thing horizontally, i will remove the y axis which will then result in the blue dot, the problem is, depending on how low/high the player is looking, the value that we will get after doing angle*100 is gonna be different everytime, the lower you look, the smaller the value is, and the more horizontal you look, the closer it is to 100.
now, if we use the cross product sleitnick told me about, the result will be the green dot, and the resulted value will always be the same, no matter how low or high you’re looking.

2 Likes

ummm… what do you mean? could you try something like this vector * power