Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Pilot · Feb 26, 2012 at 05:00 PM · physicsrigidbodygravitycomponent

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.

Comment
Add comment · Show 2
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 homer_3 · Feb 26, 2012 at 05:14 PM 0
Share

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.

avatar image Pilot · Feb 26, 2012 at 06:23 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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