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
0
Question by Rootlan · Nov 15, 2016 at 03:46 AM · physics settings

Don't get Physics.gravity

New to coding and need a little help, I making a game in wich stuff falls from above and the player has to avoid them, the problem is that I don't want the stuff falling at the same speed so I'm using this code to alter the gravity of the falling objects:

public class modGravity : MonoBehaviour {

 public float grav;
 public GameObject debreis;
 void Start()
 {
     {
         Physics.gravity = new Vector3(0, -grav, 0);
        
     }
  
 }

}

How the game works is that there's a block that's a kinematic object, when the players enters a collider that's parented to the block it stops being kinematic and starts falling. The problem is that no matter what value I asing to the grav variable the objects all fall at the same "normal" gravity speed. So is there something missing?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UNDERHILL · Nov 15, 2016 at 05:46 AM

As you are using Vector3 above and didn't specify I'll assume this is 3D Physics.

I would instead provide an opposite force against the direction the object is falling. I recommend reviewing documentation on RigidBody.AddForce()

Depending on the force you add, and how often you add it, the object will appear to fall more slowly. Instead of modifying mass or gravity you are working against it in tiny increments.

Here's some example code (which I commented for you) that I use to make a robot hover. This goes in FixedUpdate() not in Update(). By reducing the force which is added upwards below a certain threshold it will simply fall more slowly to the ground instead. I definitely think this is the way for you to go if working with 3d physics. You'll have to play with the weights of things (which should be a standard across all falling objects) and then you can just adjust the force you are adding upwards on each object to determine how slowly it falls. Think of it like adding helium balloons of various sizes to the falling object since the net effect is the counteracting of gravity.

  //hover
     Ray downRay = new Ray(transform.position, -Vector3.up); //prepare a ray to cast to the floor
     if (Physics.Raycast(downRay, out hit))  //cast a ray to the floor
     {
       //Debug.Log("Floor hit distance: " + hit.distance);
       float hoverError = 6 - hit.distance; //determine appropriate height based on a tolerance
       if (hoverError > 0) //if the bot is not hovering high enough
       {
         //Debug.Log("Hover error is: " + hoverError);
         rb.AddForce(11.0f * Vector3.up); //add a force to the bot
         CheckDistanceMove(distanceFromPlayer); //move toward the player
       }
       else if (player.transform.position.y > transform.position.y - 3) //if the player is elevated above the bot then move up regardless
       {
         //Debug.Log("pushing bot up to match player!");
         rb.AddForce(11.0f * Vector3.up); //add a force to the bot to keep it in the air
         CheckDistanceMove(distanceFromPlayer); //move toward the player
       }
     }

In particular you will want to focus on this line:

  rb.AddForce(11.0f * Vector3.up); //add a force to the bot

I highly recommend reading through ALL of the documentation in the Physics package or at a minimum browsing through all the methods in Physics so that you know what is out there. They are ALL highly useful! The world of 3D physics has broken many many a would be Unity C# developer... it broke me 3 times before I finally read ALL of the documentation before writing another line of code. It's also only about 30 pages total.

https://docs.unity3d.com/ScriptReference/Physics.html https://docs.unity3d.com/ScriptReference/Physics-gravity.html

Please let me know if you have any questions!

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

79 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I create a naturally swaying sequence of physics bones for a character? 0 Answers

I would like to better understand the physics of wheel colliders. 0 Answers

Rolling a ball when game starts 0 Answers

I'm trying to make the collision work. But... 2 Answers

Change Physics2D.maxTranslationSpeed at runtime does not work 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