效果图如下
public Transform mid;
public int imagecount;
public List< GameObject> objects;
float currentangle = 0 ;
public int distance;
private float x = 0 ;
void Start ( )
{
objects = new List < GameObject > ( ) ;
for ( int i= 1 ; i<= imagecount; i++ )
{
objects. Add ( GameObject. Find ( string . Format ( "Sphere{0}" , i) ) ) ;
}
foreach ( GameObject i in objects)
{
Debug. Log ( i. name) ;
}
Initialize ( ) ;
}
private void FixedUpdate ( ) {
float x = 0 ;
if ( Input. GetMouseButton ( 0 ) )
{
x = Input. GetAxis ( "Mouse X" ) ;
}
currentangle + = x;
currentangle % = 360 ;
Initialize ( ) ;
detect ( ) ;
}
public void Initialize ( )
{
float deltaangle = 360 / imagecount;
foreach ( GameObject a in objects)
{
Vector3 offset = mid. transform. right * distance * Mathf. Cos ( currentangle* 2 * Mathf. PI/ 360 ) + mid. transform. forward * distance * Mathf. Sin ( currentangle* 2 * Mathf. PI/ 360 ) ;
a. transform. position = mid. transform. position + offset;
currentangle + = deltaangle;
}
}
public void detect ( )
{
if ( Input. GetMouseButton ( 0 ) ) {
Ray ray = Camera. main. ScreenPointToRay ( Input. mousePosition) ;
RaycastHit hit ;
if ( Physics. Raycast ( ray, out hit) ) {
print ( "hit:" + hit. collider. gameObject. name) ;
}
}