# Reset one part (x,y or z) of the forward vector to previous value

Hey guys,
i’ve got a camera follwing a look at. For a certain situation i want to avoid the y component of the camera forward vector getting lower (camera dipping down) while the x and z component get updated using transform.LookAt(v3).

Sounds easy: Cache y, update x and z, insert old y.

Well, that doesn’t work. Here’s why: You obtain y based on the normalized forward of the transform, all values of the forward vector get updated and normalized again. Simply inserting the old y into the new x and z (which will then be normalized again) doesn’t work as normalizing will change the ratio between y and the other two components to a new normalized vector. Not sure this was understandable. But in the end: y still changes
Any idea how to achive my goal?

Daniel

P.S.: When exploring alternatives I am kind of stuck on using LookAt and - if possible - forward.

Does this mean you cannot use something like Cinemachine (free from Unity in the package manager)? It basically has this sorta thing built in, so that you can follow something but only up to a point, staying within regard bounds.

Otherwise if you insist on hacking your way through hand-rolled camera code, I’d make a proxy object that the camera tracks, and then have that proxy object (an invisible GameObject) chase the target object but never go below wherever you want the camera to gaze.

1 Like

Well, I got it. Actually all the ingredients for my answer were in my question. If anyone else ever searches for this, here is how it works:

• Store the y-component relative to a normalized Vector2 of the x and z component (or in whatever combination you are working).
• Calculate new Forward to your liking
• replace new Y with the stored Y multiplied by the new normalized Vector2 of the x and z component

Like This:

`````` float initialFwdY = transform.forward.y / new Vector2(transform.forward.x, transform.forward.z).magnitude;
transform.LookAt(yourGameObject.transform);
transform.forward = new Vector3(transform.forward.x, initialFwdY * new Vector2(transform.forward.x, transform.forward.z).magnitude, transform.forward.z);
``````

This will keep Y the same for changing X and Z.
Not sure anyone will ever need this, but you’re welcome.

Well, you always can But I started quite some time ago not knowing about this back then and I basically don’t want to “transfer” 1400 Lines of code to another Methodology.