Recently, in Unity, I tried to move a sphere along a figure-eight path. It first moves in a circle, and after completing the circle, it starts the second circle in reverse, using negative rotation. However, my plan did not work as I expected. The reason is that Unity changes negative degrees into positive degrees, so -15 degrees becomes 345 degree. Therefore, the direction of the circular movement won't change, when you flip from a positive degree to a negative degree. In other words, if the first path is clock-wise the second path (after changing the sign of the degree) will be clock-wise, too.
Is there any practical way to overcome this problem, and change the direction of rotation by changing from positive degrees to negative degrees?
Here is one of the scripts I have tried:
public class PlayerMover : MonoBehaviour
{
float x;
public float speed=100.1f;
private Transform playerTransform = null;
void Start ()
{
playerTransform = GetComponent<Transform>();
}
void Update ()
{
x=speed*Time.deltaTime;
playerTransform.Rotate(0, 5 *funct(x) * x, 0);
playerTransform.position += playerTransform.forward ;
}
public float funct(float u)
{
u=Mathf.Sin(u);
return u;
}
}
As you can see. even the function I use to flip from positive degrees to negative degrees is a smooth and continuous function, mathematically speaking.