Rotate an object around another object (422131)

I have 2 sphere prefabs Sphere1 and Sphere2 the problem is that Sphere2 doesn’t orbit Sphere1 it just does a large orbit.
Here’s the code i have tried.

var orbitspeed = 20.0;
var Sphere1Prefab:Transform; 

function Update () {
	
transform.RotateAround(Sphere1Prefab.position, Vector3.up, Time.deltaTime * orbitspeed); 
	
}

Thankyou for any help.

1 Like

Here is a cool little script someone wrote for me a while back. Just adjust a var or two to get the axis you want it to rotate around :smile:

///////////////////////////////////////////////////////////////////////////////////
var rotationMask = Vector3(0, 1, 0); //which axes to rotate around 
var rotationSpeed = 5.0; //degrees per second 
var rotateAroundObject: Transform; 
///////////////////////////////////////////////////////////////////////////////////
function FixedUpdate() { 
   if (rotateAroundObject) {//If true in the inspector orbit <rotateAroundObject>: 
    transform.RotateAround(rotateAroundObject.transform.position, 
	rotationMask, rotationSpeed * Time.deltaTime); 
   } 
   else {//not set -> rotate around own axis/axes: 
	transform.Rotate(Vector3( 
	rotationMask.x * rotationSpeed * Time.deltaTime, 
	rotationMask.y * rotationSpeed * Time.deltaTime, 
	rotationMask.z * rotationSpeed * Time.deltaTime)); 
   } 
}
1 Like

Thanks this is good, But the orbit is huge how do i do a smaller orbit…?

1 Like

As is the object orbits at the radius/distance from the transform. But you could do all kinds of things with a var or two (ie) make it move in and out from the orbits center. Or rotate around its own axis while rotating around the center transform. :idea:

What you making anyway? :smile:

1 Like

Hi thanks for your help… I’m just a newbie to this and really just making a sort or 3D/2.5D space invaders, So i’m trying to sort of create a solar system as a back drop.
While also learning as i go along so to speak.

1 Like