0
\$\begingroup\$

I'm creating a simple endless runner game, I'm dynamically generating the terrain, I'm placing obstacles at runtime at pseudo-random position. Here is the script that generates the terrain and places the obstacles:

public class Builder : MonoBehaviour {
    //Private vars
    private GameObject plane1;
    private GameObject plane2;
    private GameObject plane3;
    public GameObject obstacle;
    public GameObject coin;
    //Generates random vector3 within given range
    public Vector3 generateRandomVector(float xMin, float xMax, float zMin, float zMax){
        return new Vector3 (Random.Range (xMin,xMax),0,Random.Range (zMin,zMax)) ;
    }
    // Use this for initialization
    void Start () {
        plane1 = Resources.Load ("Plane1") as GameObject;
        plane2 = Resources.Load ("Plane2") as GameObject;
        plane3 = Resources.Load ("Plane3") as GameObject;
    }
    //Updates the position of the gameObject, this is the location where the next plane will be spawned
    public void updatePosition(){
        Vector3 newPosition = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 10);
        gameObject.transform.position = newPosition;
    }
    // This will pick up random plate and add the corresponding nnumber of other objects(coins, obstacles and powerups)
    public void generateTerrain(){
        if (Random.Range (0, 6) > 3) {
            //Clone the plane
            GameObject newPlate = Instantiate (plane3, transform.position, transform.rotation) as GameObject;
            //Generate objects
            generateObjects(newPlate, 7);

        } else if (Random.Range (0, 4) > 2) {
            //Clone the plane
            GameObject newPlate = Instantiate (plane2, transform.position, transform.rotation) as GameObject;
            //Generate objects
            generateObjects(newPlate, 5);
        } else {
            //Clone the plane
            GameObject newPlate = Instantiate (plane1, transform.position, transform.rotation) as GameObject;
            //Generate objects
            generateObjects(newPlate, 3);   
        }
        //Updating the position so that the next element won't be on the same position as this one
        updatePosition();
    }
    //Generates obstales, coins and powerups
    public void generateObjects(GameObject parent, int numOfObjects){
        for(int i = 0; i < numOfObjects; i++){
            // Generate what kind of object will be spawned
            int rand = Random.Range (0, 10);
            //Generate random location within the parent
            float xMin, xMax,zMin,zMax;
            Renderer parentRenderer = parent.GetComponent<Renderer>();
            xMin = parentRenderer.bounds.min.x;
            zMin = parentRenderer.bounds.min.z;
            xMax =  parentRenderer.bounds.max.x;
            zMax =  parentRenderer.bounds.max.z;
            Vector3 pos = generateRandomVector(xMin, xMax, zMin, zMax);
            //TODO add powerups
            if(rand < 6){
                //Instantiate obstacle
                GameObject newObstale = Instantiate(obstacle, pos, parent.transform.rotation) as GameObject;
                newObstale.transform.parent = parent.transform; 
            }else{
                //Instantiate coin
                GameObject newObstale = Instantiate(coin, pos, parent.transform.rotation) as GameObject;
                newObstale.transform.parent = parent.transform;
            }
        }
    }
}

The obstacles have tag "Obstacle" and collider, and isTrigger is checked. The problem is that when the player moves there are places where there isn't any obstacle but the OnTriggerEnter method detect a collision. Here is my class which is handling the OntriggerEnter method

public class PlayerTrigger : MonoBehaviour {
    private gameData gameDataInstance = new gameData();
    void OnTriggerEnter(Collider collider){
        if (collider.gameObject.tag == "Coin") {
            gameDataInstance.addCoins(1);
        } 
        if (collider.gameObject.tag == "Obstacle") {
            Debug.Log ("Obstacle");
            //StorePersisten.storePersistenInstance.Save(gameDataInstance);
        }
        if (collider.gameObject.tag == "PowerUp") {
            // powerup
            Debug.Log("powerUp");
        } 
    }
}

Accrding to my observations the collision happens when I call generateTerrain(Builder class) from another class called Destroyer which is destroying the objects which are behind the player. Here is the Destroyer class:

public class Destroyer : MonoBehaviour {
    private Builder builder;
    private bool isColliding;
    void Start() {
        builder = (Builder)FindObjectOfType(typeof(Builder));
        Debug.Log("Destroyer");
    }
    void Update(){
        isColliding = false;
    }
    void OnTriggerEnter(Collider collider){
        if (isColliding)
            return;
        isColliding = true;
        if (collider.gameObject.transform.parent != null) {
            Destroy (collider.gameObject.transform.parent.gameObject);
            Debug.Log ("Destroyed parent");
        } else {
            Destroy(collider.gameObject);
        }
        if (builder) {
            builder.generateTerrain ();
        }
    }
}

In short: There are collisions between the player object and object with tag "obstacle" without any visiable object around the player. Any ideas what may cause the problem?

EDIT:Forgot to mention that there's a game object "builder" which is a few units away from the player

\$\endgroup\$
  • \$\begingroup\$ What did you do to find out which object the player is actually colliding with and where it detects that collision? \$\endgroup\$ – Philipp Aug 28 '15 at 8:52
  • \$\begingroup\$ Used the debugger to see the object name, which was really useful because all obstacles have the same name. Anyway I fixed it by removing this line " newObstale.transform.parent = parent.transform; " don't know why but now works. \$\endgroup\$ – Planet_Earth Aug 28 '15 at 8:58
0
\$\begingroup\$

I cannot add just a comment because of missing reputation so I write it as answer but be aware this is not a tested answer.

You apply the transform of ur obstacles parent after initiate. So maybe it's because after the initiate of the obstacle the player collider trigger fires up, because the actual position of the collider is at the players position at initiate.

\$\endgroup\$
  • \$\begingroup\$ Forgot to mention that there's a game object builder which is a few units away from the player \$\endgroup\$ – Planet_Earth Aug 28 '15 at 8:16

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.