유니티 Gravity Scale - yuniti Gravity Scale

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Your name Your email Suggestion*

Cancel

Switch to Manual

public float gravityScale;

Description

The degree to which this object is affected by gravity.

In 2D physics, the gravity is a global setting in the Physics2D class but you can also control the proportion of that gravity applied to each object individually using gravityScale. For example, it may be easier to implement a flying character by turning off its gravity rather than simulating the forces that keep it aloft.

See Also: Physics2D.gravity.

  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges

  • Home /

0

As the title says when player jumps the gravity scale of the rigidbody2d changes. I want to set it back to its default value after player lands to the ground. How to do it? Here is my code without all the solutions I tried, so you can see everything that does work.

private Rigidbody2D rb; private BoxCollider2D boxCollider2d; public float JumpSpeed; // Player's jump speed public float downScaler; // gravity to change [SerializeField] private LayerMask platformLayerMask; void Start() { rb = gameObject.GetComponent<Rigidbody2D>(); boxCollider2d = gameObject.GetComponent<BoxCollider2D>(); } private void FixedUpdate() { Jump(); } private void Jump() { if (IsGround() && Input.GetKey(KeyCode.Space)) { rb.gravityScale = downScaler; rb.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse); } } private bool IsGround() { RaycastHit2D rayCastHit2D = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .18f, platformLayerMask); Debug.Log(rayCastHit2D.collider); return rayCastHit2D.collider != null; }

0

Answer by logicandchaos · Jan 25, 2021 at 08:27 PM

Once you touch the ground you set the gravity scale back to 1, that is the default value.

Your answer

Hint: You can notify a user about this post by typing @username

Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Related Questions

I am trying to change the gravity scale of my player object only in 3D mode, but there is no gravity scale in 3D rigidbodies.

How can you manipulate the gravity scale (or whatever it is called) for 3D rigidbodies? of the player?

I'm fairly new to unity and C# and I'm trying to understand how to change the gravity scale of my character when I press the space bar. When I debug it, it says that Rigidbody2D.gravityScale cannot be used as a method. Can someone explain why it brings this error and how to fix it?

public class PlayerMovement : MonoBehaviour { public float moveSpeed; private Rigidbody2D rb2d; public float addGrav; // Use this for initialization void Start () { rb2d = GetComponent<Rigidbody2D> (); } // Update is called once per frame void Update () { //Understanding::: If key pressed down, //Transform - method that manipulates position of object //Translate moves transform in direction/distance of translation. //transform.Translate(translation); if (Input.GetKey (KeyCode.UpArrow)) { transform.Translate (Vector2.up * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.DownArrow)) { transform.Translate (Vector2.down * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.LeftArrow)) { transform.Translate (Vector2.left * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.RightArrow)) { transform.Translate (Vector2.right * moveSpeed * Time.deltaTime); } if(Input.GetKey (KeyCode.Space)) { rb2d.gravityScale (addGrav); } } }

Tom Aranda

5,71811 gold badges32 silver badges50 bronze badges

asked Nov 30, 2017 at 1:18

rb2d.gravityScale is not a method, it's a field that you can assign value to.

use this

rb2d.gravityScale = addGrav; //to asign //other example rb2d.gravityScale += addGrav; //to add rb2d.gravityScale -= addGrav; //to substract

answered Nov 30, 2017 at 1:38

endrik exeendrik exe

5962 silver badges10 bronze badges

It's because the compiler were reading it as a method since you used parentheses instead of equal sign. it should be like so.

rb2d.gravityScale = addGrav;

answered Nov 30, 2017 at 1:39

Toplist

최신 우편물

태그