unity gravity script code example
Example: gravity script unity
// 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;
}
}