• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by teopotter88 · Dec 17, 2016 at 08:16 PM · character movementfreezeposition

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Fobri · Dec 17, 2016 at 08:36 PM

You could Disable Rigidbody and CharacterController.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image teopotter88 · Dec 18, 2016 at 08:34 AM 0
Share

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 timing. So the only thing I need help with is the freezing part. I tried your method, but It didn't work.

avatar image Laiken teopotter88 · Dec 18, 2016 at 04:15 PM 0
Share

oh. Try

 GetComponent<Animator>().enabled = false

it will make the character not move a single inch

avatar image teopotter88 Laiken · Dec 19, 2016 at 03:07 PM 0
Share

enabled doesn't work. It gives an error. But I was able to do it with another method. Thank you :)

avatar image GrKl teopotter88 · Dec 20, 2016 at 06:37 AM 0
Share

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.

avatar image
0

Answer by Bren0831 · Dec 18, 2016 at 06:32 PM

Or you could try... GetComponent= rb rb.isKinematic=true;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by KhenaB · 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;





Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image teopotter88 · Dec 19, 2016 at 03:08 PM 0
Share

Thank you for your help. I just disabled the movement component of the player.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I query the RigidbodyConstraints? 3 Answers

Unstable Rigidbody with freeze position 1 Answer

how to create a star gravity 0 Answers

Rigidbody collision movement similar to a wall 0 Answers

How to rotate character respect ScreenView 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges