Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ByteYT · Jun 21, 2017 at 03:30 PM · gravitywaterunderwater

Gravity UnderWater

Hi, I wanted to know how to make gravity under 15.2f in the y-axis, I'll paste the code and the rigidbody.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class UnderWater : MonoBehaviour {
 
     public bool UW;
     Rigidbody rb;
 
     // Use this for initialization
     void Start () {
         rb = this.gameObject.GetComponent<Rigidbody> ();
     }
     
     void Update ()
     {
         if (this.transform.position.y <= 15f) {
             new WaitForSeconds (0.2f);
             if (transform.position.y < 15f) {
                 rb.AddForce(Vector3.up * -Physics.gravity.y, ForceMode.VelocityChange);
                 UW = true;
             }
             else if (transform.position.y == 15f) {
                 rb.AddForce(0,0,0);
                 UW = false;
             } 
         }
     }
 }


https://i.gyazo.com/0c53bdc76107f1a0b30fdf8044b16feb.png

https://i.gyazo.com/e18653ce89880b15a4068d19908b7332.png

Comment
Add comment · Show 4
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 hexagonius · Jun 21, 2017 at 03:36 PM 0
Share

First, your WaitForSeconds won't work and is impossible to use and not recommended in update. Second, adding 0 force is equal to not making the call at all, you can remove it.
Third, the else if could just be an else, because everything not below 15 is above, right? Inessence, UW = transform.position.y < 15f;

avatar image ByteYT hexagonius · Jun 21, 2017 at 03:50 PM 0
Share

but I want to make the player float and that still doesn't work

avatar image Habitablaba · Jun 21, 2017 at 04:18 PM 1
Share

If you want to be realistic about it, there are buoyancy scripts out there that you can attach to objects to make them behave correctly in water.

avatar image ByteYT Habitablaba · Jun 22, 2017 at 02:56 PM 0
Share

Thanks, Habitablabla I searched and I found a buoyancy script.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by · Jun 21, 2017 at 04:53 PM

Gravity is just a global force applied to all rigidbodies that have "useGravity" enabled. Form what i see you are just trying to enable and disable the gravity when reaching y = 15.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class UnderWater : MonoBehaviour {
  
      public bool UW;
      Rigidbody rb;
  
      // Use this for initialization
      void Start () {
          rb = this.gameObject.GetComponent<Rigidbody> ();
      }
      
      void Update ()
      {
          if (this.transform.position.y <= 15f) {
              new WaitForSeconds (0.2f);
              if (transform.position.y < 15f) {
                  rb.useGravity = true;
                  UW = true;
              }
              else if (transform.position.y >= 15f) {
                  rb.useGravity = false;
                  UW = false;
              } 
          }
      }
  }

I also changed the if statement. You can not guarantee that the Y will be exactly 15 at any point.

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 · Jun 21, 2017 at 05:05 PM 0
Share

Sorry, I didn't notice the other IF.

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

72 People are following this question.

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

Related Questions

Underwater implementation 1 Answer

Change controller gravity below a certain level? 0 Answers

Making my character's gravity change when i hit a collider. 1 Answer

How do I make the area under my water (a plane) look like its underwater? 4 Answers

Swimming/Underwater 3 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