Instantiate prefab on y axis with distance below player

Hi Masters!

I very new to coding and I need help in spawning prefabs with distance below my player.

Here’s my code:

public class GameController : MonoBehaviour {
PlayerControl player;
public GameObject[ ] obstacle;
public float obstacleDistance;
public int spawnRate;
public int spawnTime;
void Start()
{
Physics.gravity = new Vector3(0, -458, 0);
player = GameObject.FindGameObjectWithTag(“Player”).GetComponent();
InvokeRepeating(“spawnObstacle”, spawnTime, spawnRate);
}
//Level
void spawnObstacle()
{
int obstacleNum = Random.Range(0, 4);
float obstacleX = Random.Range(-2, 4.5f);
float obstacleY = player.transform.position.y;
Instantiate(obstacle[obstacleNum], new Vector3(obstacleX, obstacleY - obstacleDistance, -13.0f), Quaternion.identity);
}

What happens when I run is some clone spawns above the player. I’m guessing there’s something wrong with my Instantiate?

Thanks in advance!

This doesn’t work as well:

Instantiate(obstacle[obstacleNum], new Vector3(obstacleX, obstacleY, -13.0f) - new Vector3(0, obstacleDistance,0), Quaternion.identity);

Change …

float obstacleY = player.transform.position.y;

to

float obstacleY = player.gameObject.transform.position.y;

That’s actually not required.

My apologies, it’s an early morning.

No worries. I guess the problem might be with how I can’t get the position on my player, so I can then subtract a distance between my player’s y axis, and the prefab I want to spawn.

I just copied your code and tried it. I substituted ‘PlayerControl’ for just a transform of a Sprite, obviously because I don’t have that file/code.
All of my obstacle objects spawned at the same height (which was what I set … subtracted from the player’s position.y).
Not one of them spawned at any other height, or higher than the player.

This leads me to ask if the Obstacles have any height already set on them before you instantiate them (ie: in their prefabs maybe).

Okay, I was writing as you two posted. No worries @Malleck666 .

What do you mean you can’t get the position on your player?

1 Like

I’m not sure if the code that I used to get the player’s position is correct.

float obstacleY = player.transform.position.y;

and yes I have a Y position set on prefabs.

Ok, your code is correct. Just out of curiousity, what are the Y values on the prefabs, and what is the distance you use (set in the inspector) for “below the player” ?

I think I got what your saying. I didn’t reset the positions on the prefab. My prefabs has parent/child. I already reset the position for all the parent. I can’t reset the position for the children since I need their position to be the obstacle. <-Will this cause a problem?

Well, I cannot really say 100% because I’d just be guessing as to how you have it setup.
Let me say what I was thinking and you can take it from there…

If you had +5 to the y position on a prefab., and a minus 4 on how far below you want to spawn the object, let’s say…
then when you spawn -4 + 5 you get 1 :slight_smile: Which is … (obviously) 1 above your player… and so on.

There’s no problem, so long as you keep that in mind, and adjust/set accordingly :slight_smile:

AWESOME MAN! You just solved the problem, I was messing around with the code when the problem is with the setting of the prefab. It’s working good now. I didn’t reset the X value of the children, I just set their Y’s to 0. Here’s how the spawn looks like: 3039702--227619--prefab.JPG

Thanks again!

Looks very cool & you’re welcome. Enjoy your game :slight_smile: