Script NOt working As Expected

#pragma strict

var y : float = 5;
var speed : float = 10;
var angle : float ;
function Start () {

}

function Update () 
{
for(angle = 0 ; angle <= 90 ; angle = angle + 0.01)
{
transform.Translate( 5 , y * Mathf.Sin(angle) , 5* Mathf.Cos(angle));
}
}

iwas trying to rotate a ball around a point but the numbers of x,y,z go to enormous amounts … ok ,now there could be some mistake with the y ,z but why is the x changing??? … and could you guys get me solutuion to make my sphere revolve around origin in y-plane

could you not make the ball a child of an emptyGameobject (positioned at pivot point) and use Transform.Rotate on the parent?

(If you need the orientation to be locked, do the same, but replace the ball with an emptyGameObject and attach the ball to it with a joint)

Let me know if those solutions is no good for you and ill propose an alternative.

I see a lot problems with your code:

  • Just in case you didn’t realised your for-loop does 9000 iterations
  • Mathf.Sin and Mathf.Cos don’t take an angle in degree but in radians. Unity has two factors to convert Deg2Rad or Rad2Deg.
  • All code inside Update is executed within a frame. So whatever you so it’s finished within a fraction of a second.
  • The Translate method takes offset values by which you want to translate the object. Since you pass 5 for x, each time you call the function you add 5 to the position in x-direction.

like @xandermacleod said if you want to just constantly rotate the object around another on a fix distance, it’s easier to parent the object to an empty GameObject, put the empty GO in the center of your circle and offset the actual object within the parent so it’s on your desired path. Now when you rotate the parent the child will follow a circular path around the center.

You can also use Transform.RotateAround.

If you want to use plain trigonometry you should understand it and do it the right way :wink:

#pragma strict
 
var radius : float = 5;
var speed : float = 90.0;  //90° per second
var angle : float =0;
var center = Vector3(5,0,0);
function Update ()
{
    angle += speed * Mathf.Deg2Red * Time.deltaTime)
    transform.position = center + Vector3(0, Mathf.Sin(angle) * radius , Mathf.Cos(angle) * radius );
}

Note: This would rotate the object around the point (5,0,0) and around the x-axis.