Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 domussolisortum · May 24, 2015 at 02:51 PM · velocityaddforcejumpingplatformgrounded

2D character can't jump off of platform floating in water

I have a platform, a crate, that's a non-kinematic, fixed angle Rigidbody2D with a mass of 10, floating in water. It has a BoxCollider2D. The water is a sprite with a trigger BoxCollider2D; attached to the water is a slightly modified version of this script written by boxing_rex. The crate starts in the air, lands in the water, bobs up and down, and eventually comes to apparent equilibrium with part of the crate sticking above the water's surface.

The problem I'm having is that my character can't jump off the crate when it's floating in water. The character can jump off the crate if the crate is resting on the ground (a platform with a PolygonCollider2D and no Rigidbody), but when the crate's in water, the character controller either doesn't detect that the character is grounded, or it can't add the jump force. The character's weight depresses the crate slightly further into the water, but at no point does it become possible to jump. Here are what I believe to be the relevant sections of code:

On the character controller:

 void FixedUpdate (){
      grounded = Physics2D.OverlapArea (GroundCheck1.position, GroundCheck2.position, whatIsGround);
      //[...]
 }
 
 void Update(){
 
      var lastVelocity = rb.velocity.y;
 
      //[...]
      
      if (lastVelocity == 0) {
          grounded = true;
      } else {
          grounded = false;
      }
      
      //this part makes her jump.
      if (grounded && Input.GetKeyDown (KeyCode.Space)) {
          animator.SetBool ("isGrounded", false);
          grounded = false;
          rb.velocity += (new Vector2 (0, jumpForce));
      }
      
      //[...]
      
      }

"rb" is the character's Rigidbody2D.

On the water object:

 //Floating by boxing_rex
     public float waterLevel = 0.5f;
     public float floatHeight = 0.05f;
     public float bounceDamp = 1.0f;
     public Vector3 buoyancyCentreOffset = new Vector3(0, 0.25f, 0);
     
     private float forceFactor;
     private Vector2 actionPoint;
     private Vector2 uplift; 
     
     void OnTriggerStay2D(Collider2D Hit)
     {
         if (Hit.gameObject.tag == "oldcrate"){
             actionPoint = Hit.transform.position + Hit.transform.TransformDirection(buoyancyCentreOffset);
             forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
             if(forceFactor > 0f){
                 uplift = -Physics.gravity * (forceFactor - Hit.GetComponent<Rigidbody2D>().velocity.y * bounceDamp);
                 Hit.GetComponent<Rigidbody2D>().AddForceAtPosition(uplift, actionPoint);
             }
         }
     }

I initially suspected that the problem lay in trying to add force to the character when the crate also has force added to it. However, there was another factor. The character's animation state during a jump goes through five frames dependent on her vertical velocity. When she's at rest on a normal platform, she goes back into her standing animation. But when she lands on one of these floating crates, she gets stuck in the fifth frame of her jump animation.

 if (lastVelocity == 0) {
     //but if it's zero, go right to the last frame.
     animator.SetInteger ("VerticalSpeed", 0);
 }

So that indicates to me that it's not detecting that she's grounded. And the conclusion I've drawn is that it's not detecting she's grounded because on a bobbing crate her vertical velocity never quite reaches zero. I don't know if that's the right conclusion, though.

It may sound like I've figured this out, but I don't feel that I have. So my question is, how do I make the crate actually reach zero vertical velocity, in a way that the character controller can detect after the character's landed on the crate? I think that would solve my problem. As might be apparent from my use of another person's script, I'm not very familiar with the manner in which forces are added to objects. Help is appreciated, thank you for reading.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Achieving consistent jump height regardless of vertical velocity 2 Answers

Error when trying to implement JumpPad/GravityPad type effect on CharacterController (Solved) 1 Answer

ThirdPersonCharacter script blocks trampoline jump 0 Answers

Drop in performance when checking velocity of rigidbody.,Performance drop when checking velocity of rigidbody. 1 Answer


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