- Home /
How can I make a gameobject "orbit" another
I am trying to make one game object orbit a star by having a rigidbody look at the star and the planet itself is a child of the rigidbody. heres my code:
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public Vector3 Force;
public Transform Target;
void FixedUpdate()
{
transform.LookAt(Target);
rigidbody.AddRelativeForce(Force);
}
}
the Problem is that when i use this, the rigidbody looks at the star, but it does not move sideways relatively so that it will "orbit", it simply moves sideways without turning. How can i fix this?
Hmmm many questions...
To simply orbit, you could create an empty game object GO, place the planet as a child of GO, with a position offset in one dimension equal to your radius of orbit, and place the GO under the star at 0,0,0. Then by rotating GO you "orbit" the planet. This is the fastest and cleanest solution, but may not be ideal for your use.
Otherwise you can just use math to move it in a cyclical pattern around the star's position, and call lookat every update. Not as fast, more code, but avoids potentially unwanted parenting.
Hope this helps!
the planet "hops" around in a circular fashion, how can i fix it
Answer by AlejandroGorgal · Nov 28, 2013 at 01:03 AM
You can use a Slerp for that, here's the code:
using UnityEngine;
using System.Collections;
public class GravityScript : MonoBehaviour
{
public Transform target;
void Update ()
{
Vector3 relativePos = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
transform.Translate(0, 0, 3 * Time.deltaTime);
}
}
And here's the tutorial that explains how to use it: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/quaternions
Updated URL to tutorial: https://learn.unity.com/tutorial/quaternions
Answer by numberkruncher · Nov 28, 2013 at 02:31 AM
Not sure if this would achieve what you want, but the easy option might be to:
Create the following object hierarchy:
Star |--- Planet
Position the planet relative to the star at desired distance.
Create a script which simply rotates Star since Planet will appear to orbit.
Answer by vargata · Nov 28, 2013 at 02:35 AM
here is a little sample code. if you add a rigidbody to your object and play with drags you can set up a pretty orbit. would be better modelling the forces
#pragma strict
var target : Transform;
private var dir : Vector3;
private var forward : Vector3;
private var side : Vector3;
private var cross : Vector3;
function Start () {
forward = transform.forward; //forward vector just to have it (in 3d you need a plane to calculate normalvector, this will be one side of the plane)
dir = target.transform.position - transform.position; //direction from your object towards the target object what you will orbit (the other side of the plane)
side = Vector3.Cross(dir, forward); //90 degrees (normalvector) to the plane closed by the forward and the dir vectors
}
function Update () {
dir = target.transform.position - transform.position;
transform.LookAt(dir.normalized); // look at the target
rigidbody.AddForce(dir.normalized * 50);//add gravity like force pulling your object towards the target
cross = Vector3.Cross(dir, side); //90 degrees vector to the initial sideway vector (orbit direction) /I use it to make the object orbit horizontal not vertical as the vertical lookatmatrix is flawed/
rigidbody.AddForce(cross.normalized * 50); //add the force to make your object move (orbit)
}
you need the drag value to work against these forces or your object will fly away on a spiral the bigger the drag the smaller the orbit range
what could i set the Variables to to make a nice orbit? my planet is 21 meters away from the star. The star is 5 meters in diameter and the planet is 1
well, i don't know, i'm new to unity, i just put 10 as mass and 0.1 to drag. but my planets size is 60 and 100. :) that made a pretty orbit for me
you dont have to give them a value, they are set up in the Start function.
Your answer
Follow this Question
Related Questions
Simple Planet orbit 3 Answers
How to predict orbit based on time? 1 Answer
LookAt doesnt work with cardboard SDK or any camera movement 0 Answers
Planet shadow flicker and shake when orbiting their star. 0 Answers
Axial tilt and moons 1 Answer