• 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 /
  • Help Room /
avatar image
1
Question by Dragonsong · Jan 23, 2016 at 01:03 PM · rigidbody2dvelocitycollision detection

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/

Comment
Add comment · Show 5
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 incorrect · Jan 23, 2016 at 01:04 PM 0
Share

And how is he supposed not to fall into water? What should stop him? :)

avatar image Owen-Reynolds · Jan 23, 2016 at 04:51 PM 2
Share

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.

Show more comments
avatar image JedBeryll · Jan 23, 2016 at 08:37 PM 1
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :)

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 Owen-Reynolds · Jan 24, 2016 at 05:07 PM 0
Share

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Why is a gameobject moves at start? 0 Answers

how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers

how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer

How do I make my character move on the Z axis on 2d rigidbody? 0 Answers

How to have the same speed on Rigidbody2D in all resolutoins ?? 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