📜  重力脚本统一 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:51.659000             🧑  作者: Mango

代码示例1
// Keep in mind that this solution is for custom physics.
// The easy way to add gravity is to just slap a RigidBody component 
// on your objects.

using UnityEngine;

public class MyObject : MonoBehaviour
{
    public Vector3 speed;
    public float gravity = 9.8f;

    // Start() is called before the first frame
    void Start()
    {
        speed = new Vector3(0f, 0f, 0f);
    }

    // FixedUpdate() is called at regular time intervals independent of frames
    void FixedUpdate()
    {
        speed.Set(speed.x, speed.y - gravity * Time.fixedDeltaTime, speed.z);
        transform.position += speed;
    }
}