Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by RandomUser123 · Aug 04, 2015 at 10:17 AM · gravityjumping

Changing one GameObjects gravity

I have a character who jumps in my game, currently when he jumps, I set Physics.gravity to -25 so that he will fall to the ground faster but unfortunately, this affects every object in the scene.

Is there a way I can just affect the gravity of the character jumping? This is the code I'm using currently:

     //If the character is grounded, keep the gravity normal, otherwise, increase it
     if (isGrounded)
     {
         Physics.gravity = new Vector3(0, -9.8f, 0);
     }
     else
     {
         Physics.gravity = new Vector3(0, -25.0f, 0);
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Hellium · Aug 04, 2015 at 10:37 AM

I guess you don't have the basis of physics.

What make you stand still on the earth and not go into the ground / fly into the air ? force compensation.

To make something be "less pushed" by a force, just add a force in the exact opposite direction with a certain "strength".

But for your problem, you should consider applying a constant force to your object only. And, Unity provide a component called .... Constant Force !

 // C#
 GetComponent<ConstantForce>().force = new Vector3(0, -25.0f, 0);

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

Answer by NeverHopeless · Aug 04, 2015 at 10:52 AM

You can consume RigidBody.gravityScale property as well. Try like:

 GetComponent<Rigidbody>().gravityScale = -0.55f;

Set this value as per your need.

Comment
Add comment · Show 6 · 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 fonko · Jul 02, 2018 at 04:05 AM 0
Share

should this only change one gameobject's gravity? or it will affect each object in the scene??

avatar image fonko · Jul 02, 2018 at 04:07 AM 0
Share

actually i've just checked for this property and it's not there anymore :(

avatar image murchu27 · Jan 10, 2019 at 02:52 PM 0
Share

Definitely the simplest way of adjusting gravity that I've seen. No messing around with external forces, just a simple one line adjustment! Kudos!

avatar image Felina · Feb 27, 2020 at 05:56 PM 0
Share

Thank you sir!

avatar image answerthegangsta · Jul 10, 2020 at 12:01 AM 0
Share

Ok so I have this

using System.Collections; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; using UnityEngine;

public class GravityTrigger : $$anonymous$$onoBehaviour { public float Rigidbody;

 void OnTriggerEnter(Collider collider)
 { Physics.gravity = GetComponent<Rigidbody>().GravityScale = -0.55f; }

}

However I'm getting an error saying that Rigidbody does not contain a definition for GravityScale. Any idea on how to fix this?

avatar image murchu27 answerthegangsta · Jul 10, 2020 at 08:42 AM 0
Share

As some have mentioned, it looks like this property doesn't exist (anymore?) for Rigidbody components in the 3D physics engine - only for Rigidbody2D components in 2D physics!

Also, be $$anonymous$$dful of case sensitivity. Even if the property did exist, you actually have a typo in your code - GravityScale should be gravityScale!

avatar image
1

Answer by AubryStrawberry · Aug 04, 2015 at 10:50 AM

As mentioned above you can apply a constant force, though if all you're looking to do is have a quick descent after reaching the jump's peak...

Why not have a jumping state, possibly triggered by a GroundCheck collider (explained in the 2D Character Controller tutorial: https://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers) though you may already have such a state check in place.

Then, while in that jumping state, you can monitor the rigidbody.velocity.y for when it reaches zero (the jump peak) and then apply a single downward force however you like.

 [SerializeField] float downScaler = 5f;

 void FixedUpdate()
 {
     if(jumping && rigidbody.velocity.y < 0f) // still jumping and falling
     {
         rigidbody.AddForce(Vector3.down * downScaler);
     }
 }
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

30 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

Related Questions

How would I limit jumps to 1 until the player touches the ground again?,What do I need to add to limit the jumps to 1 until the player hits the ground again? 0 Answers

Problem of gravity 1 Answer

Trying to limit air velocity 2 Answers

Slowing down a fall from a Jump 4 Answers

Jumping script WONT WORK!!!!!!!! for 2D 1 Answer


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