how to set a value at transform.localPosition.y

Hi there

I just stuck in an annoying thing…
I have the following code line:

transform.localPosition = Vector3.Lerp(transform.localPosition,  desiredPos, damping * Time.deltaTime);

I would like my object to move only in two axes X and Z.
So, how possible is to set a value at the Y axes in order to be fixed? I tried the following in the update method but id doesn’t work

transform.localPosition.y = 0.5

Any suggestions???

If you’re using C#, you can’t directly modify the members of a Vector3 struct, so you’ll need to create a temporary holding variable first.

Vector3 localPos = transform.localPosition;
localPos.y = desiredHeight;
transform.localPosition = localPos;

alternately:

transform.localPosition = new Vector3(transform.localPosition.x, desiredHeight, transform.localPosition.z);
2 Likes