- Home /
How do I make an object orbit another object
Someone on here said use the transform.RotateAround() but that's all the hints they gave to another user. Since I am new to scripting entirely I'm finding it difficult to put the pieces together on my own. When I think I'm starting to grasp it, I try it out and it pops up a ton of red errors.
like the example script in Unity's reference. transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime); that works well to rotate the object around the whole world, but I want it to orbit an object, so I tried adding the orbiting object as a child of the object I wanted it to orbit and then gave the child object the follow script to follow the main object but it just sat there, and didn't move at all.
also I didn't understand the script supplied in reference because
transform.RotateAround is the function right? and then the options are (Vector3.zero, Vector.up, 20 * Time.deltaTime);
so I thought, the three things are the vector3? (x,y,z) and so setting it to (2,5,20); would tell it to rotate around in a chaotic orbit, but instead it just yelled at me and said it didn't understand what I was trying to tell it.
Code doesn't yell. ;) Edit your question to supply the code you've written so far (the relevant parts of it, that is), then we can help you correct it.
I have no official code. just a large sphere and a small sphere that I want to orbit the large sphere. Any code I try, I get rid of when it doesn't work :) like I just tried this
function Update () {
GameObject.Find ("Planet2").transform.RotateAround;
}
and it said "Expression in statements must only be executed for their side-effects."
I figured this would tell it "find Planet2, then rotate around it" that's how I'd read it if I was a computer.
when I tried this one, both spheres vanished from the screen ...
function Update () {
GameObject.Find ("Planet1").transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);
}
(i changed the item to planet 1 because i had that backwards) but then I made them both separate objects so that one isn't the child and that makes planet 1 fly around in huge circles it seems but not around my planet close enough.
Answer by Lantral · Oct 11, 2011 at 09:28 AM
Looks like you need a kind of point gravitation, which can be better done with forces like this (my noob-version could for sure done more elegant):
var GravIntensity : float = 1;
function OnTriggerStay (other : Collider) {
if (other.attachedRigidbody) {
var dist = Vector3.Distance(gameObject.transform.position , other.transform.position);
other.attachedRigidbody.AddForce( (gameObject.transform.position - other.transform.position)/dist/dist*GravIntensity);
}
}
Put the script to the attracting objects and give them a trigger-sphere. Then all objects within the "GravitationSphere"(for reducing the calculation needed) with a physical body (ridgetbody) will be attraced. Hope it helps.
Ah - and I almost forgot to mention: And after that you give the moving object a soft forward push on awake. Then (if the gravitation and the movmentspeed is correct it will get on a circle - or more possibly - a elyptic path around the attractor).
Your answer
Follow this Question
Related Questions
RotateAround and Character Controller 1 Answer
Rotating a character's velocity? 3 Answers
unknown axis of rotation 1 Answer
Rotate vector around vector? 2 Answers