Need vector to raycast horizontally from charactercontroller

I want to shot a ray from my character, but in its horizontal movedirection as I tried to illustrate here:

How do I get this Vector?

Solved:

Vector3 fwd = transform.TransformDirection(Vector3.forward);
fwd = new Vector3(fwd.x, 0, fwd.z);
fwd.Normalize(); //if needed

I know that you figured this out already, but I wanted to clean up your code to be more efficient:

// Get forward vector
Vector3 v = transform.forward;

// "Flatten" the vector
v.y = 0f;

// Normalize if unit magnitudes are needed
v.Normalize();

How awkward is this?

I thought v.y = 0f; is javascript only???

Thanks very much!

1 Like

You can freely modify components of vectors in C#!
(As well as modify the components of quaternions, but that’s not very useful :stuck_out_tongue: )