- Home /
Having Trouble with Instantiating an object on an axis
In my little project, I want to spawn spikes on the y-axis at a random position above the view of the main camera. I also want to spawn the object on the sides of the screen. So far, I have gotten the object to spawn, but it spawns in the middle of the screen and in the view of the camera instead of on the sides and above. here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnSpikes : MonoBehaviour
{
Vector2 cameraPos;
public GameObject twoSpikes;
private Vector2 screenSize;
Vector2 spawnPositions;
Vector2 yValue;
Vector2 xValue;
Vector2 maxY;
void Start()
{
cameraPos = Camera.main.transform.position;
xValue.x = cameraPos.x + screenSize.x;
yValue.y = cameraPos.y + screenSize.y;
maxY.y = yValue.y + 10;
screenSize.x = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f;
screenSize.y = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
spawnPositions = new Vector2(xValue.x, Random.Range(yValue.y, maxY.y));
Debug.Log(screenSize.x + cameraPos.x);
}
private void Update()
{
Instantiate(twoSpikes, spawnPositions, Quaternion.identity);
}
}
Answer by UniluckStudios · Nov 11, 2017 at 03:04 AM
Ok I figured it out. Here is my new code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnSpikes : MonoBehaviour
{
Vector2 cameraPos;
public GameObject twoSpikes;
private Vector2 screenSize;
Vector2 spawnPositions;
Vector2 yValue;
Vector2 xValue;
Vector2 maxY;
void Start()
{
cameraPos = Camera.main.transform.position;
xValue.x = cameraPos.x + screenSize.x;
yValue.y = cameraPos.y + screenSize.y;
maxY.y = yValue.y + 10;
screenSize.x = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f;
screenSize.y = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
spawnPositions = new Vector2(xValue.x, Random.Range(yValue.y, maxY.y)); //I changed this line
Debug.Log(screenSize.x + cameraPos.x);
}
private void Update()
{
Instantiate(twoSpikes, spawnPositions, Quaternion.identity);
}
}
I changed the line mentioned above to:
spawnPositions = new Vector2(xValue.x + (screensize.x 2) 0.5f, Random.Range(yValue.y, maxY.y));
//Now everything works
Answer by michi_b · Nov 11, 2017 at 02:24 AM
so the first issue here is the order of execution. keep in mind, the start event is only called once. in that code, you first set xValue.x to cameraPos.x + screenSize.x, but at that point, you haven't written anything to screenSize yet, so it will have its default value (0, 0). therefore, that position will just be the camera's position. similar issue with the y coordinate, but at least the random offset starting from the camera position should work but the randomization only happens once at the start.
i don't really get what you want to achieve here, so i'll guess. A new instantiated spike at a fresh randomized position above the top left and top right corner of the camera at each frame? then the code should be something like this:
using UnityEngine;
public class SpawnSpikes : MonoBehaviour
{
public GameObject twoSpikes;
private void Update()
{
Vector2 topLeftViewPortPosition = new Vector3(0f, Screen.height, 0f);
Vector2 topRightViewPortPosition = new Vector3(Screen.width, Screen.height, 0f);
InstantiateSpikeAtRandomHeight(topLeftViewPortPosition);
InstantiateSpikeAtRandomHeight(topRightViewPortPosition);
}
private void InstantiateSpikeAtRandomHeight(Vector3 viewPortStartPosition)
{
Vector3 startPosition = Camera.main.ViewportToWorldPoint(viewPortStartPosition);
Vector3 spawnPosition = startPosition + Vector3.up * Random.Range(0f, 10f);
Instantiate(twoSpikes, spawnPosition, Quaternion.identity);
}
}
Ok, I tried your code and the spikes spawned somewhere, but I couldn't see them. The code also made my camera act weird. $$anonymous$$y game is a 2D game. So far, I have made a object that bounces around in the screen and goes up whenever I press the space bar. I have also made the camera's y position be equal to the object's y position. The problem I am trying to solve is making an object spawn at the right and left edge of the screen and above the camera's orthographic view so that the player cannot see the object being spawned. I can figure out the random positioning and stuff later, I just need/want to know how to make the object spawn in the correct area.