Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 leonalchemist · Apr 26, 2012 at 02:00 AM · 2dcharacter controller

floating on water physics

In this 2D platform game im using the character platform controller to move around my character. I made this box of water and when u get in u should be able to swin in, sink, and float and move on the surface unless when i press down i ca swin underwater again.

i already have most of it done, my main problems are when im on the surface, i have 2 colliders for the water, one in general and another just for the surface, my 3 problems are, i cant jump when floating, cant go underwater either, the floating system is far from what im looking for.

just showing some example of wat im using to make the character float on water and jump, im using the 'MoveTowards' function to force the player to float on the surface but as u cant see its using all axes instead of only the y axes because i dunno how to fix that

 function OnTriggerStay (hit : Collider)
 {
     if(hit.gameObject.tag == "WaterSurface")
     {
         WaterSurfaceTouch = true;
         movement.gravity = 0;
         movement.maxFallSpeed = 0;
 
         if (WaterSurfaceTouch == true)
         {
             gameObject.transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
         }
 
         if(Input.GetButtonDown ("Jump"))
         {
             myNewVelocity = Vector3(0,26,0);
             setVelocity.SetVelocity(myNewVelocity);
         }
     }
 }

any kind of tip would be nice

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Apr 26, 2012 at 02:29 AM

It seems this code would push the player towards the center of the trigger. If you want to just push it to the water height, use Mathf.MoveTowards instead:

  if (WaterSurfaceTouch == true){
    transform.position.y = Mathf.MoveTowards(transform.position.y, target.position.y, speed * Time.deltaTime);
  }
This will move the player only vertically to the same target y coordinate (whatever target is).
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 leonalchemist · Apr 26, 2012 at 01:15 PM 0
Share

oh, i was using Vector3 and $$anonymous$$athf at the same time... right thx that fix that part; and forgot to mention using that code for some reason doesnt work as well as it should; because as u can see i've set the maxFallSpeed and gravity to 0, but i still dont remain on the surface most of the time, i either sink or move upwards, wats the cause of this?

I was thinking its because the character has some inertia thats why i set the movement.velocity = Vector3(0,0,0); OnTriggerEnter

[EDIT]ok nvm, change lots of things and the overall system a little, works 99% right.... edit again, scratch that, works 100% :)

avatar image
0

Answer by pudd1nG · Apr 26, 2012 at 02:42 AM

I would suggest adding force based on how submerged the object is. However because you're using a CharacterController not a rigidbody you will need to apply the .Move() function instead of .AddForce().

Without writing this for you, take the depth (height) of the water, get the distance between the bottom of the character and the bottom of the water (probably via raycast) and apply an upward movement based on the distance. It will take a little tuning to get it to look right but is the nicest way outside of rigidbody controllers to achieve a bobbing in water effect.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Collision between Character Controller and Box Collider 2 Answers

2D Character controller that moves in increments. 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How can I remove inertia from character controller? 0 Answers

2d and character controler 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