Problems adding values for position x, y, z with a vector3

Can someone help me with following code? I don’t understand why this only happens to y. The values added for x and z is not replacing the current values of x and z, which is good. So why not the same for y?

{
player = GameObject.Find("Mouse");
transform.position = player.transform.position;
// prints x: 2.1, y: -8.2, z: 11.2

transform.position = player.transform.position + new Vector3(1.0f,
2.0f, 1.0f);
//prints x: 3.1, y: 2, z: 12.2 (correct for x and z, but not for y)

}```

-8.2 + 2 = -6.2 not 2

@Putcho I know, that’s the problem. I’m not getting -6.2 im getting “2”. No problems with x or z, only y

Clean up your data flow and pull everything out into temporary variables so you can reason about it and print it out at each step.

var pos = player.transform.position;

pos += new Vector3( whatever);

transform.posittion = pos;

then you can track down where things are going wrong better.

1 Like

if
transform.position = player.transform.position;
// prints x: 2.1, y: -8.2, z: 11.2

then your player is at -8.2 when the code is done

somewhere somehow it set your player Y to 0

before
transform.position = player.transform.position + new Vector3(1.0f,
2.0f, 1.0f);

I dont think so, there is nothing in between.

after this “y” is equal to -8.2:
transform.position = player.transform.position;

In between here is nothing that sets it to zero

then after this y becomes 2, while z and x becomes +1:
transform.position = player.transform.position + new Vector3(1.0f, 2.0f, 1.0f);

i would like to see the console debug result

void Start()

{

        player  = GameObject.Find("Mouse");

        transform.position = player.transform.position;

        debug.Log("P1: " +player.transform.position);
        debug.Log("T1: " +transform.position);
      


        transform.position = player.transform.position + new Vector3(1.0f,

        2.0f, 1.0f);

        debug.Log("P2: " +player.transform.position);
        debug.Log("T2: " +transform.position);
      
}
1 Like

Okey, i think I figured it out. It seems to be a bug in the inspector. Player Y was set to -8.2 in inspector, but when I manually set it back to 0 my character/player didn’t move on Y. So it’s like it was already on 0, but the output was -8.2.

Seems a bit strange/buggy

Does your player use CharacterController? If so, always call Move() to move it.

Does your player use Rigidbody? If so, always call .MovePosition() to move it.

In both cases above, NEVER directly change transform.position. You are startling the fish.