- Home /
Freezing character on place when health is 0?
I am creating a 2D game. When the health is 0, I want the player to freeze, so even when we hit the buttons, the character doesn't move. How can I do it?
Answer by GrKl · Dec 19, 2016 at 07:32 AM
Or you can just disable the script/component handling the movement when the player 'dies' (hp < 0). I think this really is the cleanest way.
Answer by Khena_B · Dec 19, 2016 at 10:57 AM
You can freeze the Rigidbody position like so:
Rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY;
And you can remove all the constraints like so:
Rigidbody2D.constraints = RigidbodyConstraints2D.None;
Thank you for your help. I just disabled the movement component of the player.
Answer by Laiken · Dec 17, 2016 at 09:03 PM
Put a condition before the code for player movement
If (health > 0)
{
// code that makes the character move
}
I have a code that makes my player move. When health = 0, even if the controller presses the buttons on the keyboard, the player doesn't move. I already know how to set the ti$$anonymous$$g. So the only thing I need help with is the freezing part. I tried your method, but It didn't work.
oh. Try
GetComponent<Animator>().enabled = false
it will make the character not move a single inch
enabled doesn't work. It gives an error. But I was able to do it with another method. Thank you :)
Your problem here was probably that you did
if (health = 0)
{
//allow to move
}
When you should of done:
if (health > 0)
{
//allow to move
}
especially if your 'health' variable is a float, it is probably not exactly = 0. If the above still does not work, you have a serious 'bug' :) Even though my previous answer that you accepted seems better, I wanted to clarify that @Laiken answer was good too.
Answer by Bren0831 · Dec 18, 2016 at 06:32 PM
Or you could try... GetComponent= rb rb.isKinematic=true;