Velocity holding after supposed to be set to Zero

So I have an object that I need to move up and down repeatedly between two locations, so I have the following script to make it stop after it reaches it’s low point.

public class MoveDown : MonoBehaviour
{
    public Rigidbody rb;
    public float downForce = 500f;
    public float stop = 0f;
    void OnEnable()
    {
        rb.AddForce(0, -downForce * Time.deltaTime, 0, ForceMode.VelocityChange);
    }
    // Update is called once per frame
    void Update()
    {
        if (rb.position.y < stop)
        {
            rb.velocity = Vector3.zero;
            rb.angularVelocity = Vector3.zero;
        }
    }
}

And then obviously the reverse for making it go up, I have them separate cause I’ve had problems of the object just rocketing off if I have it as the same script. But for some reason, when this script gets disabled my object just starts to resume with it’s downwards velocity that should’ve been canceled out. Completely default rigidbody aside from gravity being turned off. I use an almost identical code for moving an identical object forwards and backwards and that works completely fine. Anyone know what’s going on? Version is Unity 2021.3.15f1

You’re adding a huge force on a rigidbody. Whether it rockets off is all about its mass, drag and other parameters.

Your problem that its going downwards always is cos your force vector’s Y component is -500 * deltaTime. Where else would it go?

Ok, I see that I probably could’ve been clearer about what’s happening. So what happens is it goes to it’s assigned position, then the velocity canceling in the update triggers and it stops, then when I turn the component off to allow the MoveUp component to move it upwards the downwards force comes back immediately

Yes so why would the downward force be anything else than down when this line is adding a downward force

rb.AddForce(0, -downForce * Time.deltaTime, 0, ForceMode.VelocityChange);

The script is not going to do anything else than to add the downward force on OnEnable, then set velocity to zero once it’s far enough down.

If you want to change the direction every time, rename your “downForce” to “force”, then add this to the end of your OnEnable:

this.force *= -1f;

This will reverse the force, which in practice is your force Vector’y Y component, the whole vector is (0, force, 0). It would be better to change the type of this variable to Vector3 = (0, 500, 0). The reverse code will work all the same.

Ah ok. Thank you