- Home /
Rotate an Instantiated GameObject a set amount from another instantiated GameObject?
Hi,
I'm trying to instantiate a set number of GameObjects with a rotation of (0, 72, 0). My problem is that all the instantiations of the original are all of the same rotation, when I want them to be 72 degrees apart from each other. How would I do this?
int i = PlayerPrefs.GetInt("Number of Igloo Cameras");
if (n != i)
{
n++;
Quaternion j = Quaternion.Euler(0, 72, 0);
Instantiate(firstSpoutCamera, transform.position, transform.rotation * j);
}
Thanks.
Answer by Hellium · Nov 23, 2018 at 09:41 AM
int instancesCount = PlayerPrefs.GetInt("Number of Igloo Cameras");
Quaternion instanceRotation = transform.rotation;
Quaternion deltaRotation = Quaternion.Euler(0, 72, 0) ;
for( int i = 0 ; i < instancesCount ; ++i )
{
instanceRotation = instanceRotation * deltaRotation ;
Instantiate(firstSpoutCamera, transform.position, instanceRotation);
}
Hi, thanks for this.
I've got another problem though, I'm using a int++ to tell the block of code when to stop executing, and that was the part that was working in my code posted above. When I added this, it creates far too many instances.
For example, if I set it to 5, it would keep instantiating until the int++ hit 5 and would stop. This new code creates more than that.
Code:
int i = PlayerPrefs.GetInt("Number of Igloo Cameras");
if (n != i)
{
n++;
Quaternion j = transform.rotation;
Quaternion dj = Quaternion.Euler(0, 72, 0);
for (int a = 0; a < i; ++a)
{
j = j * dj;
Instantiate(firstSpoutCamera, transform.position, transform.rotation * j);
}
}
Since you did not provide the whole code, it was pretty hard to give you the solution that fit your needs.... Think about it next time ;)
I've updated my answer (and your question, I've added the code you were using)
Yep. Well, ideally all I need now is a single instantiation per interval of n.
Or better yet, if the player prefs bit is, say, 5, it only instantiates 5, if it's 9, it instantiates 9.
Also please name your properties better. Its gonna turn out for you and others much better. See Hellium's example its way better to name his properties little longer but self-explaining rather then using shortcuts like "j" or "dj"