How do I set velocity in just one direction?

By far this is the absolute biggest pain in the ass moving to C# from JS; that I can’t just say —.velocity.x = 5.

I know how to set the velocity using a temporary Vector3, ie. —.velocity = new Vector3(x,y,z), but the problem with this method is that in order to move in just one direction I have to set the other directions to 0. But I don’t want it to always be zero. I want to be able to jump, move in several directions at once, etc.

From what I can tell I can’t just set a temporary int like I can the Vector3, and if I can I can’t figure out how.

So in C#, what’s the proper way to just set velocity.x/y/z?

I’m no pro at Unity physics but I believe you could use AddForce to add force in a certain direction to move.

But if you want to just set the individual velocity values directly, you can do something like this…

If your Rigidbody reference is stored in “rb”, and you only want to affect Y axis your new velocity for Y is “newVelocityValue”:

rb.velocity = new Vector3(rb.velocity.x, newVelocityValue, rb.velocity.z);

That essentially says to set the Y axis to the new value, and set the x/z to their current values. You can read rb.velocity.x/y/z , you just can’t set them individually.

This is along the lines of what I’ve been doing, but this poses problems when trying to move in multiple directions at the same, jumping while moving. Setting a value to the current velocity while also trying to change that velocity in a different line is conflicting.