- Home /
C# Rotate More than Two GameObjects
I've been trying to figure out a way more than two gameobjects from RotatingGameObjects. i and i + 1 should go through RotatingGameObjects.length and move all those gameobjects accordingly. but for some reason it rotates only the first two gameobjects within RotatingGameObjects. Is there a hole in my logic?
using UnityEngine;
using System.Collections;
public class RotateGameObjects : MonoBehaviour {
public float fDistance = 1;
public float fSpeed = 2;
public float sensitivity = 3F;
public GameObject[] RotatingGameObjects;
void LateUpdate () {
for(int i = 0; i < RotatingGameObjects.Length - 1; i++){
RotatingGameObjects[i].transform.RotateAround(transform.position, Vector3.forward, - fSpeed / fDistance);
RotatingGameObjects[i + 1].transform.RotateAround(transform.position, Vector3.back, - fSpeed / fDistance);
}
}
}
Try placing a debug.log statement in the for loop. It should log 3 times. If it is, then your transforms are already at the required rotation, or the rotation can't be seen. If that isn't the case, then something else is wrong.
And also, I think you should use i += 2 ins$$anonymous$$d of i++ in your for loop.
Answer by fafase · Dec 20, 2014 at 05:31 PM
void LateUpdate () {
for(int i = 0; i < RotatingGameObjects.Length - 1; i++){
RotatingGameObjects[i].transform.RotateAround(transform.position, Vector3.forward, - fSpeed / fDistance);
RotatingGameObjects[i + 1].transform.RotateAround(transform.position, Vector3.back, - fSpeed / fDistance);
}
}
I don't quite get the point here:
you move the first guy forward and the second backward.
you move the second forward (back to 0) and the third backward
third goes forward (back to 0) and fourth move backward
As a result only the first and last are actually moving.