Help changing the z component of relative velocity

Hello,

I’m able to access the z component of relative velocity, I just can’t change it. I’m trying to make it stay at zero.

thisVelocity = transform.InverseTransformDirection(rigidbody.velocity);
var zVelocity : float = thisVelocity[2];

if(zVelocity != 0.0)
{
    zVelocity = 0.0;
}

When you read the float out of the Vector3, you are copying it into another float

so…

var zVelocity : float = thisVelocity[2];

This COPIES the value. It doesn’t reference the value. It is a value, not a reference.

You need to copy it back into the Vector3.

so

if (thisVelocity[2] != 0.0)
{
   thisVelocity[2] = 0.0;
}

This copies the value of “0.0” into the vector.

When you assign zVelocity to the value of the vector, you then have two values. If you change one, the other doesn’t change.

You need to copy them into each other if you want to make them the same… if you get.

I feel like I’m explaining this badly.

See this: Value vs Reference Types in C#