How to detect when an object stops moving

I am making a pool game and am trying to make it switch Players when the ball stops moving.

this is what I have tired but it is not working

Please let me know,

Thank you for your time

private void SpawnPlayer(bool IsPlayerTeam)
    {
        if(IsPlayerTeam)
        {
            PlayerCue.transform.position = Vector3.MoveTowards(transform.position, CueTransform.position, m_CueSpeed);
        }
        else
        {
            PlayerCue.transform.position = new Vector3(0, 0, -20);
        }
    }

    private void SpawnOponent(bool IsOponentTeam)
    {
            if(IsOponentTeam)
            {
                OponentCue.transform.position = Vector3.MoveTowards(transform.position, CueTransform.position, m_CueSpeed);
            }
            else
            {
                OponentCue.transform.position = new Vector3(0, 0, 20);
            }
    }

    void Start()
    {
        SpawnPlayer(true);

    }

    private void SwitchPlayer(bool SwitchTeam)
    {
        if (CueBallRB.velocity.sqrMagnitude < .01)
        {
            if (SwitchTeam)
            {
                Cue.SetActive(true);
                SpawnPlayer(false);
                SpawnOponent(true);
            }
            else
            {
                Cue.SetActive(true);
                SpawnOponent(false);
                SpawnPlayer(true);
            }
        }
    }

You need to call SwitchPlayer from within an Update function. Now your method is never invoked.

There appears to be some key logic here that’s missing so I’m not sure how the rest of your scene is put together. I’m assuming you have working logic for after SpawnPlayer() is called where the player can aim and hit the ball.

I would reccomend that once the ball starts moving, a bool like ballIsMoving should be set to disable any other action by the players so we can watch the shot play out.

Once that bool is set, the following update loop should take place:

void Update() {
  if (ballIsMoving) {
    if (ball.rigidBody.velocity <= 0.05f) {
      ball.rigidBody.velocity = 0f;  // stop the ball
      ballInPlay = false;
      SwitchPlayer(true);            // switch player
    }
  }
}