How to adjust gravity in unity

introduce

How to adjust gravity in unity


global

If you want to globally adjust gravity in your Unity project, you can do so by modifying the project's Physics Settings. Specific steps are as follows:

Open the Unity editor, select the "Edit" menu, then "Project Settings" -> "Physics".

In the Physics Settings window, you can see the settings of various physical properties, including gravity, air resistance, friction, etc. Find the "Gravity" setting.

Modify the value of Gravity to globally adjust the gravity effect of objects in the project. The default value is -9.81, which means standard acceleration due to gravity. If it is set to 0, all objects will have no gravity effect. If it is set to a positive value, all objects will have an upward gravity effect.

Note: Modifying gravity settings will affect the gravity effect of all objects, so it needs to be done with caution. If you only want to modify the gravity effect of an object, you can use the above method to modify the gravity coefficient of the object separately in the code.


screenplay

In Unity, the gravity of the object can be adjusted by modifying the gravityScale property of the Rigidbody component. The gravityScale property defaults to 1, which means standard gravity acceleration. If it is set to 0, the object will have no gravity effect, if it is set to 2, the object will have 2 times the standard gravity effect.

The gravity of an object can be modified by the following code:

public Rigidbody rb; // 获取物体的刚体组件
public float gravityScale = 1f; // 设置重力系数

void Start()
{
    
    
    rb = GetComponent<Rigidbody>();
    rb.gravityScale = gravityScale;
}

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/130037527