0
\$\begingroup\$

I'm making an infinite runner game and I'm trying to spawn obstacles, but all of them float far above the ground instead of being at the ground level.

How the summoned obstacles float above the ground when the game is running.

This is the code on summoning the bears.

PlatformGenerator Class

    if (Random.Range (0f, 100f) < randomEnemyThreshold) 
        {
            GameObject newEnemy = enemyPool.GetPooledObject();

            Vector3 enemyPosition = new Vector3 (0f, 0f, 0f);

            newEnemy.transform.position = transform.position + enemyPosition;
            newEnemy.transform.rotation = transform.rotation;
            newEnemy.SetActive(true);
        }

Ask me more about the game if you need more information.

Edit:

Here is the whole class.

The PlatformGenerator Class

using UnityEngine;
using System.Collections;

public class PlatformGenerator : MonoBehaviour {

public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;

private float platformWidth;

public ObjectPooler theObjectPool;

public float randomEnemyThreshold;
public ObjectPooler enemyPool;

// Käytä alkuarvon alustamiseen
void Start () {
    platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;


}

// Päivitys tulee joka ruutu
void Update () {

    if (transform.position.x < generationPoint.position.x) 
    {
        transform.position = new Vector3 (transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);

        //Instantiate (thePlatform, transform.position, transform.rotation);
        GameObject newPlatform = theObjectPool.GetPooledObject();

        newPlatform.transform.position = transform.position;
        newPlatform.transform.rotation = transform.rotation;
        newPlatform.SetActive (true);

        if (Random.Range (0f, 100f) < randomEnemyThreshold) 
        {
            GameObject newEnemy = enemyPool.GetPooledObject();

            Vector3 enemyPosition = new Vector3 (0f, 0.5f, 0f);

            newEnemy.transform.position = transform.position + enemyPosition;
            newEnemy.transform.rotation = transform.rotation;
            newEnemy.SetActive(true);
        }
    }

}

}
\$\endgroup\$
  • \$\begingroup\$ Your enemy prefab probably just needs to have gravity added to it. Make sure you have a RigidBody attached to the enemy prefab with gravity set up properly. \$\endgroup\$ – jgallant Mar 23 '16 at 10:09
1
\$\begingroup\$

Try checking the collider component on the game objects you are spawning. Add a rigid body component on them too as they are using gravity !!

\$\endgroup\$
  • \$\begingroup\$ Thank you. That worked. I just switched to OnCollision instead of OnTrigger and locked Z and X position. But the problem is now that the enemies spawn too close to each other and you can't jump over them. \$\endgroup\$ – SleepySleeper Mar 25 '16 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.