Question by
MiiiFLY · Sep 11, 2020 at 12:14 AM ·
errornot workingupdate function
orbit gravity,
hello can some help me i need to creat orbit gravity , don`t know way this don`t working planet not gravit to the sun he fly Velocity 5f -y on one way,Hellow im need to creat orbit gravity in unity . Can some one help way planet can`t gravit to the sun? using System.Collections; using System.Collections.Generic;
using UnityEngine;
public class Graivty : MonoBehaviour { public GameObject sun; private Vector3 sunPosition; public GameObject planet; private Vector3 planetPosition; public Rigidbody planetRigibody; private float planetMass = 10.0f; private float sunMass = 100000.0f;
Vector3 startVelocity = new Vector3(0f, 5f, 0f);
void Start()
{
planetRigibody.AddForce(startVelocity, ForceMode.VelocityChange);
}
void Update()
{
planetRigibody.AddForce(calculateForce(), ForceMode.Impulse);
}
public Vector3 calculateForce()
{
sunPosition = sun.transform.position;
planetPosition = planet.transform.position;
float distance = Vector3.Distance (sunPosition, planetPosition);
float distanceSquared = distance * distance;
float G = 6.67f * Mathf.Pow(10, -11);
float force = G * sunMass * planetMass / distanceSquared;
Vector3 forceWithDirection = (force * (sunPosition-planetPosition));
return (forceWithDirection);
}
}
Comment