简体   繁体   中英

Unity 3d C# Stop player movement on death (falling down)

I have an small 3d game with a ball that are going only in front and jump. I want to stop the player movement when the ball are falling down but i don't know.

 void FixedUpdate()
{
  // Player movement
  rb.transform.Translate(0, 0, forwardForce * Time.deltaTime,);

  // What's happening when the ball fall down
  if (rb.position.y < -1f)
  {
    FindObjectOfType<GameManager>().EndGame();
  }

}

Try to use condition to check if the movement of the ball before moving the player.

void FixedUpdate()
{
  // What's happening when the ball fall down
  if (!(rb.position.y < -1f))
  {
    // Player movement
    rb.transform.Translate(0, 0, forwardForce * Time.deltaTime,);
  } else
  {
    FindObjectOfType<GameManager>().EndGame();
  }

}

Worked with this code, thanks:

 if (rb.position.y > -1f)
  {
    // Player Movement
    rb.transform.Translate(0, 0, forwardForce * Time.deltaTime);
   }
  else
  {
    FindObjectOfType<GameManager>().EndGame();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM