Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Pooths · Oct 11, 2014 at 02:17 PM · c#2dcharactercontroller

Fall damage

Hello there community. I am trying to make a 2D puzzle platformer but I am having some problems with my "fall damage" script. The code works, sometimes... I have no idea why it doesn't work all the time.

When a player falls from a high platform and get a vertical velocity of over 80 I assign the variable lethalSpeed to true, the problem is that you can shift gravity while in mid-air so even if you at one point during the airtime have lethalSpeed you might survive if you gravity shift and land on a platform before you reach lethalSpeed.

I have been trying to debug the code with Debug.Log, but it seems it doesn't enter for example respawnI() even if the conditions in the if-statement are met(should be met).

I would apreciate any feedback figuring this out or if I can improve anything. And as I said, it works sometimes.

 private void checkPlayerState()
    {
         if (!grounded)
         {
             currentSpeed = Mathf.Abs(rigidbody2D.velocity.y);
             if (currentSpeed > 80f)
                 lethalSpeed = true;
             else
                 lethalSpeed = false;
         }
         if (grounded && lethalSpeed)
         {
             respawn ();
         }
     }


 private void respawn()
     {
         gameObject.transform.position = currentCheckPoint;
         currentSpeed = 0;
         lethalSpeed = false;
     }
 






EDIT: Problem solved!

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

1 Reply

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

Answer by MrSoad · Oct 11, 2014 at 03:21 PM

It could be that you are hitting the ground causing your speed to drop below 80f before your death check kicks in. This will reset your lethalSpeed to false. So you are on the ground but not dead. Maybe switch the Death check "if (grounded && lethalSpeed)" section to before the "if (!grounded)" section to avoid this.

Comment
Add comment · Show 20 · 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 MrSoad · Oct 11, 2014 at 03:17 PM 0
Share

I would prob be doing this sort of state change check in the on collision function. This way it will trigger when the event happens rather than when you code activates in Update or wherever you are calling it from. Which could affect you "grounded" state check.

avatar image Pooths · Oct 11, 2014 at 03:41 PM 0
Share

I have used Debug.Log(currentSpeed) and it shows that the speed is higher than the threshold and I only change the value of currentSpeed while in the air. Also the character hits the ground after the groundcheck does, so currentSpeed is never decreased before jumping again.

avatar image MrSoad · Oct 11, 2014 at 03:48 PM 0
Share

I could do with seeing more code. If you are not calling checks from on collision events then your actual character state may be different from you codes current best information regarding your character state. Post more code and I will have a better look for you.

avatar image Pooths · Oct 11, 2014 at 03:51 PM 0
Share
 Layer$$anonymous$$ask whatIsGround;
 
 
 void Update()
         {
             grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
             checkPlayerState ();
     
         }

This is the groundcheck, I have used this type of groundcheck throughout my project, but as you say "onCollisionEnter2D" might be a better choice in this situation

avatar image MrSoad · Oct 11, 2014 at 04:10 PM 0
Share

Sorry for the code formatting, any help or advice on posting code better is very welcome. I don't see a 101010 button anywhere, or any buttons or formatting options(using Firefox). Please put me on the right path if you have a moment thanks!

Show more comments

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Top Down Character being Pulled down and movement script not working? 2 Answers

Object name not found? 1 Answer

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 1 Answer

How to make character move up walls and along the ceiling? 0 Answers

I only want my character to jump when touching the ground 4 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