I’m having an issue with the below script not working. The sun and planet are not rotating as they should. I got this code from a tutorial online from a different site. This tutorial was written in 4.0 and I have unity 5.0. I have a feeling something in 5.0 is different and is why it is not working. The planet and sun are both prefabs, don’t know if that makes a difference. I have them a prefabs so later down the road I can click on screen and add planets or suns. I just want to know what I’m doing wrong in the code below. Any help would be greatly appreciated, please let me know if you need anything further.
using UnityEngine;
using System.Collections;
public class Orbiting : MonoBehaviour {
public Transform Sun;
public Transform Planet;
// public static
//public Rigidbody rb = UnityEngine.Component.GetComponent<Rigidbody>();
void Awake()
{
Rigidbody rb;
rb = GetComponent<Rigidbody>();
Planet = transform;
rb.AddForce(transform.forward * 100);
rb.AddForce(transform.up * 100);
}
// Use this for initialization
void Start () {
GameObject sun = GameObject.FindGameObjectWithTag("Sun");
Sun = sun.transform;
}
// Update is called once per frame
void Update () {
Rigidbody rb;
rb = GetComponent<Rigidbody>();
Vector3 line = Sun.position - Planet.position;
line.Normalize();
float distance = Vector3.Distance(Sun.position, Planet.position);
rb.AddForce(line * 10 / distance);
}
}