Collision detection for the next frame
I've got the following situation: While my character is in water it can move on the Y axis and gravityScale is set to 0, but the moment OnTriggerExit2D gets called he's considered out of the water and gravityScale is set back to its value.
The problem is that when the character is at the top of the water pool it can move on the Y and gets out of the water the gravity is set and the character drops in the water again this whole process creates a flickering of the character.
So I want to check in OnTriggerStay2D if the next frame the character will be out of the water and set the movement.y to 0 before Exit gets called/
And how is he supposed not to fall into water? What should stop him? :)
There isn't any way to look-ahead in the actual collision system (how could there be, since it doesn't know where you'll be going?) Unity has things like Physics.OverlapSphere where you can just pick any spot and check for overlap. People use that for "will my spawned object fit here."
But this sounds like a design problem. If you jump out of the water, you should fall back in. Just not right away. Whatever makes you fall into the water in the next frame -- that's your problem.
I was thinking about Rayman when the player is in water he can only move upwards if he isn't already at the top. And can go out only by jumping out of the water. So I guess I wanted that behavior - you can move along the Y axis only if you aren't at the top.
Also I can get the player's rigidbody velocity I thought I can somehow calculate where he should end up the next frame.
Thank you for your comment i will try playing around with OverlapSphere
I don't think you should turn off gravity. Ins$$anonymous$$d you could add a constant upward force while under water that gets stronger the deeper you are. So i would check where the water level y is and where my character position y is and the force would be: waterLevel - charLevel + gravity. Or you could just simply add an impulse type force to the rigidbody when you get inside the water to make it jump out.
Thank you I will try the constant upwards force !
Answer by Dragonsong · Jan 24, 2016 at 04:43 PM
Just for reference if someone stumbles upon this in the future.
I solved my problem by creating a child object under my character positioned just below the middle of the character with a BoxCollider2D trigger in my water pool script if this object triggers OnTriggerExit2D then a flag AtWaterPoolTop is set to true and movement on Y is restricted. If OnTriggerEnter2D then the character can swim upwards again :)
The more common way this sort of thing is done is having a short raycast going straight down from the feet. Not sure it's better in this case.
Your answer
Follow this Question
Related Questions
Player randomly stops moving 1 Answer
[Not solved] RigidBody2D sticks to walls 0 Answers
rigidbody.velocity.x doesn't work :( 2 Answers
Trying to add force to an object... is this done correctly? 0 Answers
Projectile Changing Angle BEFORE Impact 0 Answers