Reputation: 421
I'm trying to spawn objects one by one with +0.6 space on Y axis. Objects should be 0.6, 1.2, 1.8, 2.4, 3 etc while it looks like this 0.6, 1.8, 3.6, 6, 9 etc.. I dont know whats going on so i hope you can help me, here's a code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject cubeOne, cubeDouble, cubeTriple, cubeQuattro, cubeOneShort, cubeDoubleShort, cubeTripleShort, cubeQuattroShort, sphereOne, sphereDouble, sphereTriple, sphereQuattro, sphereOneShort, sphereDoubleShort, sphereTripleShort, sphereQuattroShort;
int whatToSpawn;
float position;
int yRotation;
void Update () {
whatToSpawn = Random.Range(1, 5);
position += 0.6f;
Vector3 newPosition = transform.position;
newPosition.y += position;
switch (whatToSpawn)
{
case 1:
Instantiate(cubeOne, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 2:
Instantiate(cubeDouble, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 3:
Instantiate(cubeTriple, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 4:
Instantiate(cubeQuattro, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
}
}
}
thank you for answers.
Upvotes: 0
Views: 1027
Reputation: 350
You add position
to newposition
and increase position
by 0.6 each time and assign it to the spawning object. So what I see is like that:
loops | position | newposition | p + np = result
-------------------------------------------------------
1 | 0.6 | 0 | 0+0.6 = 0.6
+0.6 .-------------------↵
↓ ↓
2 | 1.2 | 0.6 | 1.2+0.6 = 1.8
+0.6 .-------------------↵
↓ ↓
3 | 1.8 | 1.8 | 1.8+1.8 = 3.6
And so on...
So I think it is just mathematical. What Fish proposed solves it therefore.
Upvotes: 2
Reputation: 1039
Assuming you are trying to use the Instantiate overload public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
.
The code in the Instantiate is moving the transform.position of the spawner. Change your instantiate code to:
Instantiate(cubeOne, newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Upvotes: 5