- Home /
Game objects are getting instantiated at the same position.
So my player is moving along the Y axis downwards but not at a constant or predictable rate (meaning the player can even stop moving ). The camera is following the player so I decided to use it to set a location for spawning game objects along the y axis. I used a Yoffset to make it spawn objects ahead of the camera and a DifferenceInDistance variable to try and maintain a distance between game objects, however, I'm still having game objects appearing at the same position. Please Help thanks :)
public class ObjectSpawnner : MonoBehaviour {
public float Yoffset;
public GameObject [] obstacles;
public int NumberOfGameObjects;
public float startTime;
public float RepeatRate;
public float DifferenceInDistance = 4;
float YDistance = 0;
// Use this for initialization
void Start () {
InvokeRepeating ("GenerateRandomObjects",startTime ,RepeatRate );
}
void GenerateRandomObjects(){
float x =obstacles [0].transform.position.x;
float y = GameObject.Find ("Main Camera").transform.position.y;
int r = Random.Range (0,NumberOfGameObjects);
YDistance += DifferenceInDistance;
Debug.Log ("Y distance is " + YDistance );
Instantiate (obstacles [r], new Vector3 (x, y - Yoffset - YDistance, 0), Quaternion.identity);
}
}
@nickkorage What do you mean they spawn at the same position? Are they spawning at the same xy coordinates? Or do you mean that they always spawn with same distance from each other?
$$anonymous$$y question simply is how do I spawn game objects while maintaining a certain distance from each other? Some instances in my game I'm finding the game objects being spawned together or not keeping the distance I intended from each other.
also not sure. from the looks YDistance is an ever increasing value while yOffset is an unknown value and both get subtracted from the current camera y. moving the camera down it should instantiate objects ever further away from it including it's speed. If it stays and only the character moves down, they should appear along his easy. that's how I read this, but I would like to hear the answer to $$anonymous$$ister-$$anonymous$$ortal's question too
The player is not moving at a specific rate. The player's movement is unpredictable and the camera is following the player. I wanted the game objects to be spawned separately from each other maintaining a certain distance from each other but it's not happening. In some few cases I found the game objects (Which are not moving in any direction) spawning together or near each other. I can't account for this discrepancy.
Answer by hexagonius · Nov 25, 2016 at 10:17 AM
So I ran your code as is and it works as I described, spawning cubes all the way downwards without any hickups.
The only fact that this could fail is that it constantly uses the Main Camera position.y and you said you were moving it, but then only if the camera goes up, since the offset extends down. I'd put the spawner on a gameobject that stands still and starts in the same position as the camera, OR you save the starting position of the camera and keep using that y instead
Yeah, it works as long as the camera goes down. Problem arises when the player stops moving or goes up(Camera is following player) How would you suggest I go about spawning game objects while retaining the gap between the game objects bearing in $$anonymous$$d that the player/camera might stop moving or move up?
I would save the cameras start position at start and use it throughout the game ins$$anonymous$$d of the changing camera position
Your answer
Follow this Question
Related Questions
Spawning multiple objects in same area without intersecting each other 0 Answers
how to stop Instantiate after 3 minuts,trying to stop Instantiate after 3 minuts 2 Answers
Translate a 2D position to a 3D space 1 Answer
instantiated GameObject Prefab jumps in air after instantiate 1 Answer
How can I naming each instantiated object in "for" loop ? 1 Answer