Changing one value in a Vector3?

I’m building a mech which can strafe in all directions while keeping the cockpit pointed at the mouse, but when the legs would change direction the cockpit (which was a child) would rotate a little. So instead of having them as parent/child, I have them separated and am using the below code to keep them together. Problem is, I need the y axis to be 1.5units higher.

    void Update() {
        transform.position = LightLegsDirectional.LegsPosition;
    }

I tried searching around and can’t work out how to do it.

EDIT: I fixed the problem by making a ghost object in the position it needs to be. Which seems really obvious now. I’m curious as to how changing what I needed originally is done though.

1 Like

You cannot modify any of the 3 axis directly. You have to create a new Vector3 and assign transform.position to that Vector3. For example, to modify the y value only:

Vector3 newPosition = new Vector3(transform.position.x, someNewYValue, transform.position.z);
transform.position = newPosition;
2 Likes

Ahhhh! That makes sense. Thanks heaps!

Well, you can.

1 Like

You can the individual components of a Vector3 variable as you linked to, but not on a transform, which is what the OP is using.

Right, because you’d be modifying your local copy.