- Home /
How can I turn on/off Rigidbody.useGravity?
I have put together a simple rigidbody motor and am trying to have the Rigidbody's "Use Gravity" turned off (false) while the character is grounded, and turned on (true) when they are not grounded. Here is my motor script, it is written in C#:
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour
{
public float forwardSpeed = 10.0f;
public float backwardSpeed = 6.0f;
public float sidewaySpeed = 8.0f;
public float jumpForce = 10.0f;
public float airModifier = 0.8f;
public void FixedUpdate()
{
bool grounded;
//When the user presses the W or UpArrow key, move them forward at the forward speed
Vector3 forwardMovement = Input.GetAxis("Forward") * transform.forward * Time.deltaTime * forwardSpeed;
//When the user presses the S or DownArrow key, move them backward at the backward speed
Vector3 backwardMovement = Input.GetAxis("Backward") * transform.forward * Time.deltaTime * backwardSpeed;
//When the user presses left or right (or "a & "d") on the keyboard, move them side ways at the sideways speed
Vector3 sidewaysMovement = Input.GetAxis("Horizontal") * transform.right * Time.deltaTime * sidewaySpeed;
//Is the character grounded?
if (Physics.Raycast(transform.position, -transform.up, 2)) {
grounded = true;
Rigidbody.useGravity = false;
} else {
forwardMovement *= airModifier;
backwardMovement *= airModifier;
sidewaysMovement *= airModifier;
grounded = false;
}
//Jump when the user presses the space key AND the character is grounded
if (Input.GetKeyUp("space") && grounded)
{
rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
Rigidbody.useGravity = true;
}
//Move the character
transform.Translate(forwardMovement + sidewaysMovement);
transform.Translate(backwardMovement + sidewaysMovement);
}
}
I am getting 2 errors about each of the "Rigidbody.useGravity" lines since I'm not sure how exactly to access the compenent correctly: An object reference is required to access non-static member `UnityEngine.Rigidbody.useGravity'
Would appreciate help with how I can fix these two lines. Thank you.
Are you familiar with object oriented program$$anonymous$$g? The error tells you the exact problem. You are trying to access the useGravity member variable directly through the Rigidbody class. You need to use an object of type Rigidbody and modify that object's useGravity member variable. When you add a Rigidbody component to an object in your scene, Unity will add a Rigidbody member variable for you to access called rigidbody. So you should be doing rigidbody.useGravity = false.
Not really. If I had understood the error log I wouldn't be posting a question here. I hadn't realized the subtle difference between using "Rigidbody" and "rigidbody", so thank you for picking that up. Cheers bud
Your answer
Follow this Question
Related Questions
Physics gravity appears very weak 2 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
How to make a object jump constantly at y and move to the next position to z (perfectly) 0 Answers
My objects keep falling through 1 Answer
How can I match Unity's gravity implementation per object? 1 Answer