My question is fairly simple and I have come up with a solution but cannot seem to get the basic requirement done, basically I want an object to rotate between two angles, for example if the gameobject in question is at 0 degrees in y, then it should first rotate y to 45 degree; and after that it should rotate to -45 degrees, thus continuously alternating between these two angles.
The solution which I came up with was to use Mathf.Sin function to alternate between the angles since the angle is common i.e 45 I will use the positive max and negative max of sine wave to achieve this, but can't seem to get it work. The code in Update is as follows:
Vector3 startPos = transform.position; // umm, start position !
Vector3 targetPos = Vector3.zero; // variable for calculated end position
float startAngle = -theAngle * 0.5f; // half the angle to the Left of the forward
float finishAngle = theAngle * 0.5f; // half the angle to the Right of the forward
// the gap between each ray (increment)
float inc = (theAngle / segment);
RaycastHit hit;
// linecast between points
if ( Physics.Linecast( startPos, targetPos, out hit ) )
{
Debug.Log( "Hit " + hit.collider.gameObject.name );
}
// to show ray just for testing
Debug.DrawLine( startPos, targetPos, Color.green )