- Home /
Orbiting bodies with unity 5.0
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);
}
}
Okay, I'm trying to get two planetary bodies to orbit each other. I'm starting real small. Ultimately I hope to get a planet sim, where you can click and add either suns or planets and depending where you click they will either orbit, fall into, or leave the solar system. But I can't seem to get this script to work. I followed a youtube tutorial online. However, this youtube video was made for 4.0ish version of unity. 5.0 has a bunch of changes I'm still trying to learn when using the tutorials. the planet and sun are prefabs, however, in the unity program I have the sun and planet marked with the relevant model in the program. IE sun to the sun transform and the planet to the planet transform. The program is not orbiting like it should, it actually isn't orbiting at all, it is just sitting there, and I can't seem to figure out why. I also did a search for this issue, but couldn't find quite what I was looking for, so I'm going to try and ask, and hope I don't get trolled to harshly. This script was written for 5.0, just in case I wasn't clear.
NOTE
Figured out what was going on. I had marked the objects with 'is Kinematic' and that what was causing them to not respond to the orbiting script. I don't know why that was, but that what was happening. Thank you all for your help. Feel free to add suggestions.
Answer by Jessespike · Jun 09, 2015 at 09:41 PM
Not sure if you really need physics, but a simple orbit can be done without using the physics system.
public class Orbit : MonoBehaviour {
public Transform OrbitPivot; // The transform that this object will orbit around
public float OrbitSpeed = 100f;
// Update is called once per frame
void Update () {
this.transform.RotateAround(OrbitPivot.position, Vector3.up, OrbitSpeed * Time.deltaTime);
}
}
Well, the physics portion is so I can build up later, and make something really cool with collisions and the such. I did find out that un-checking the 'Is $$anonymous$$inematic' for the prefab allowed the object to move. I got to fine tune the actual rotation bit. I was thinking adding a scroll bar to increase or decrease velocity so you can either get a rotation or a crash. But I do not know why the 'is $$anonymous$$inematic' check box would cause the rotation not to happen. Thank you for the quick reply.
Answer by Dave-Carlile · Jun 09, 2015 at 09:31 PM
void Start () {
GameObject sun = GameObject.FindGameObjectWithTag("Sun");
sun = sun.transform;
}
This part of your code is problematic. You've declared a local variable called sun
, which has the same name as your Orbiting
class variable called sun
. This in itself isn't bad, but when you have sun = sun.transform
in your Start
method you're just assigning to your local variable sun
. I'm surprised that actually compiles since you're assigning a Transform
to a GameObject
. But maybe Unity has an implicit cast for that?
To fix this you need to either rename your local variable (probably the best option). Or you can assign to the class variable like this:
this.sun = sun.transform
This
tell's Unity that you're referring to the object instance scope, so it will look for the sun
variable there instead of the local one you've created.
There may be other issues, but that's what jumped out at me.
You are correct, I got lazy with the na$$anonymous$$g, I need to fix that. If I add more then one prefab, is there a way I can get the script to update with every orbiting body present in the scene? The local variable is a quick adjustment. You are correct I need something general purpose to allow for more then one planet or sun to interact with the other objects in the scene. Working with Unity is neat but a slight learning curve to be sure. Thank you for your reply.
You should just be able to put this script on each object that is orbiting?
Oh ok, so I was on the right track to attaching them to the prefabs? Because I want to ultimatly be able to click some where on screen and have a planet or sun appear and start to interact with the other object given gravity and velocity and the such.