- Home /
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);
}
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);
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.
should this only change one gameobject's gravity? or it will affect each object in the scene??
actually i've just checked for this property and it's not there anymore :(
Definitely the simplest way of adjusting gravity that I've seen. No messing around with external forces, just a simple one line adjustment! Kudos!
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?
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
!
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);
}
}
Your answer

Follow this Question
Related Questions
Problem of gravity 1 Answer
Trying to limit air velocity 2 Answers
Slowing down a fall from a Jump 4 Answers