首先,你需要先模拟好重力
usingUnityEngine;
usingSystem.Collections;
publicclassPlanetGravity : MonoBehaviour
{
//The gravity of the planet
publicfloatgravity = 10;
privateTransform m_transform;
voidAwake()
{
m_transform = transform;
}
publicvoidAddGravity(Transform targetObject)
{
//The gravity direction of the planet
Vector3 gravityDirection = (targetObject.position - m_transform.position).normalized;
//Add the gravity to the target object
targetObject.GetComponent().AddForce(-gravity * gravityDirection);
}
这个脚本目前单纯用来产生星球重力
利用 RigidBody.AddForce 这个API
来产生对物体中心的力
以此来模拟星球重力
接下来建立 PlayerGravity.cs 脚本
1
2
3
4
5
6
7
8
9
10
1