Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

avatar image Dragonsong Owen-Reynolds · Jan 23, 2016 at 05:05 PM 0
Share

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

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.

avatar image Dragonsong JedBeryll · Jan 24, 2016 at 07:30 AM 0
Share

Thank you I will try the constant upwards force !

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.

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

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


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