- Home /
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.
Your answer
Follow this Question
Related Questions
Achieving consistent jump height regardless of vertical velocity 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Collision on the bottom? 1 Answer
3d Side Scroller - Cutting of velocity when characters energy hits zero.. 1 Answer
Jumping with rigid body3d 1 Answer