- Home /
Check if gravity is inverted
I need my character to jump down (negative y value) if the gravity is inverted. How do I script this? The gravity is multiplied with -1 when I press a button. So how do I make an if statement for when the gravity is inverted? Any help would be greatly appreciated!
Answer by FlaSh-G · Jun 20, 2017 at 08:41 AM
You can read the gravity value and check if it is positive or negative, but it's usually cleaner to just have a boolean lying around. If we're talking global gravity (as in "not just some property in a single object"), a static variable mith come in handy in whatever class you're switching gravity.
public static bool upwardsGravity = true;
public void InvertGravity()
{
// Gravity float * -1 here
upwardsGravity = !upwardsGravity;
}
And then you go
ThatScriptsName.upwardsGravity
wherever to ask about inverted gravity.
Thank you, that worked! The problem I have now however is that the character moves not only forward but slightly sideways when falling back down. What have I done wrong now?
There is no way to tell without knowing which components you use and what your code is.
$$anonymous$$akes sense, sorry. This is my code. Could the rotation affect the position?
void Start() { //rb = GetComponent(); //jump = new Vector3(0.0f, 2.0f, 0.0f);
// get the third person character ( this should never be null due to require component )
character = GetComponent<ThirdPersonCharacter>();
StartCoroutine(Choreography());
}
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$eyDown ("down") && upwardsGravity == true)
transform.position += new Vector3(0,2,0) * jumpSpeed;
if (Input.Get$$anonymous$$eyDown ("down") && isGrounded == false)
InvertGravity();
if (Input.Get$$anonymous$$eyDown ("down"))
transform.Rotate (transform.rotation.x,transform.rotation.y,transform.rotation.z-180);
if (Input.Get$$anonymous$$eyDown ("down") && upwardsGravity == false)
transform.position += new Vector3(0,-2,0) * jumpSpeed;
}
void LateUpdate()
{
if (Input.Get$$anonymous$$eyDown ("down"))
InvertGravity();
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Ground"){
grounded=true;
}
}
void OnCollisionStay()
{
isGrounded = true;
}
IEnumerator Choreography()
{
yield return new WaitForFixedUpdate ();
//walk forward
float startTime = Time.time;
float duration = 120f;
while (Time.time < startTime + duration) {
character.$$anonymous$$ove (Vector3.forward, false, false);
yield return new WaitForFixedUpdate ();
}
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
if statement only ever triggering the first condition even if it's false and the second one is true 0 Answers
how to keep a "ship" over a platform or rigidbody? 0 Answers
controller.Move doesn't stay grounded when walking down slope 3 Answers