G
G
Gregory2562021-09-12 16:32:57
C++ / C#
Gregory256, 2021-09-12 16:32:57

How to stop an object when it reaches a certain coordinate?

How to stop an object when it reaches a certain coordinate? For example, Y needs to stop the object. I give the object an impulse, upon reaching the value 0, I would like the object to stop. However, when pressed, the object should continue to move.

if(transform.position.y.Equals(0))
        {
            

            if(Input.GetKeyDown(KeyCode.Space))
            {
                playerRb.AddForce(Vector3.forward * playerSpeed, ForceMode.Impulse);
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gregory256, 2021-09-12
@Gregory256

Found a solution if anyone needs it

if(transform.position.y.Equals(0))
        {
            playerRb.constraints = RigidbodyConstraints.FreezePosition;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                playerRb.constraints = RigidbodyConstraints.None;
                playerRb.AddForce(Vector3.up * playerSpeed, ForceMode.Impulse);
            }
        }

UPD
If there is a more interesting solution, I will be glad to see your options

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question