- Home /
Moving an ob around a sphere
I would like to move an object (lets say a 2d triangle ) around a sphere. The following code results the triangle behave in a strange way.
void Update ()
{
if (Input.GetAxis ("Horizontal") > 0)
{
abs_rotation += 0.1f;
}
if (Input.GetAxis ("Horizontal") < 0)
{
abs_rotation -= 0.1f;
}
if (Input.GetAxis ("Vertical") > 0)
{
this.rigidbody.AddForce(this.transform.forward * 10);
Debug.Log("Up");
}
//I store the center of the sphere in a varriable
Vector3 spherePos = GameObject.Find("Sphere").transform.position;
//I calculate what would be the position of the object on the surface of the sphere
//Distance represents the radius of the sphere
Vector3 posVect = this.transform.position-spherePos;
posVect.Normalize();
posVect *= DISTANCE;
//I put my triangle to the right position and rotate i to the appropriate direction
this.transform.position = posVect;
this.transform.up = posVect;
this.transform.RotateAround(posVect,abs_rotation);
}
The problem is that my triangle behaves as if the bottom of the sphere was pulling it. Does anybody has any idea?
Answer by aldonaletto · Sep 28, 2013 at 05:41 PM
Is this your actual code? It should not even compile, since RotateAround requires 3 parameters: the center of rotation, the axis about which to rotate and the angle. RotateAround modifies the position and rotation of the rotating object so that it keeps facing the center - modifying these properties while rotating isn't a good idea, and may cause the weird behaviour you're describing. Another point: Find'ing an object every Update is terribly slow: Find it once at Start and save its position in a member variable instead. The code for rotating the object about the sphere could be something like this:
Vector3 center; // save the sphere center here
void Start(){ // Find sphere only once:
center = GameObject.Find("Sphere").transform.position;
}
void Update ()
{
if (Input.GetAxis ("Horizontal") > 0)
{
abs_rotation += 0.1f;
}
if (Input.GetAxis ("Horizontal") < 0)
{
abs_rotation -= 0.1f;
}
// rotate around sphere about world Y
this.transform.RotateAround(center, Vector3.up, abs_rotation);
}
Notice that abs_rotation is the rotation speed: this code rotates continuously around the sphere, and the left and right arrows just increase/decrease the speed. If you want to rotate only when these keys are pressed, change the code in Update to this:
void Update ()
{
float rotAngle = Input.GetAxis ("Horizontal") * 0.1f;
// rotate around sphere about world Y
this.transform.RotateAround(center, Vector3.up, rotAngle);
}
NOTE: I removed the Input.GetAxis("Vertical") code just because it was screwing up the formatting due to some mysterious and unknown reason - keep it in your code if you want.
First of all it compiles but I corrected the RotateAround function call as you suggested. If try the line "float rotAngle = Input.GetAxis ("Horizontal") * 0.1f;" nothing happens it doesn't rotate at all.
Sorry to be correct it rotates but only one unite an when I release the left or the right button it rotates back to the original direction automatically
You're right about RotateAround: there's an obsolete and undocumented version that accepts 2 arguments (although I could not understand what exactly it does). I tested my scripts and both did what I expected: abs_rotation behaved like the rotation speed in the first one, while the second one made the object rotate around the sphere only when the horizontal keys were pressed - but it didn't return to the original direction when they were released. $$anonymous$$aybe something else in your script is restoring the original rotation.