Search code examples
c#unity-game-engineunityscriptgameobject

Stuck with spawning objects in Unity


I'm making a 'run' game in Unity and I'm making a prototype with a ball, that has other balls following him. If the followers hit an object they get destroyed after some time. To make it so you don't run out of enemies I made a trigger that spawns new enemies. In the code this is the function Addzombies.

  1. How do I make them spawn not on the same point, if i run it now they start on eachother and bounce around as an explosion.
  2. How do i make some start in the air, i tried it but they don't spawn.

My code:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    public float InputForce;
    public GUIText guiText;
    public float rotationHorizontal;
    public AudioClip ACeffect2;
    public GameObject zombiePrefab;

    void FixedUpdate() {

        rigidbody.AddForce( Camera.main.transform.right * Input.GetAxis("Horizontal") * InputForce);
        rigidbody.AddForce( Camera.main.transform.forward * Input.GetAxis("Vertical") * InputForce);

        transform.position += Vector3.forward *InputForce * Time.deltaTime;
        rotationHorizontal = Input.GetAxis("Horizontal") * InputForce;
        rotationHorizontal *= Time.deltaTime;
        rigidbody.AddRelativeTorque (Vector3.back * rotationHorizontal);

    }

    void OnCollisionEnter(Collision col){
        if (col.gameObject.name == "Zombie") {
            Debug.Log ("Player geraakt, nu ben je eigenlijk dood");
        }
        if (col.gameObject.name == "Obstakel1") {
            Debug.Log ("Obstakel1 geraakt");
            audio.PlayOneShot(ACeffect2);
            InputForce = 0;
        }
        if (col.gameObject.name == "Obstakel2") {
            Debug.Log ("Obstakel2 geraakt");
        }
    }

    void AddZombies(int aantal){
        for (int i = 0; i < aantal; i++){
            GameObject go = GameObject.Instantiate(zombiePrefab, transform.position - new Vector3(0, 0, 7 + i),Quaternion.identity) as GameObject;
            Zombie zb = go.GetComponent<Zombie>();
            zb.target = gameObject.transform;
        }
    }

    void OnTriggerEnter(Collider col) {
        Debug.Log ("Enter" +col.name);
        if (col.tag == "AddZombies"){
            AddZombies(4);
        }
    }

    void OnTriggerExit(Collider col) {
        Debug.Log ("Leaving with" +col.name);
    }
}

Solution

  • I will advice on how things could be done, but you will have to make changes to make it suit your requirement

    public Transform zombiePrefab; // From the editor drag and drop your prefab
    
    
    void addZombies()
    {   
        // as you need them to be not on the same point
        int randomX = Random.Range(-10.0F, 10.0F);
        for (int i = 0; i < aantal; i++){
            // make a transform
            var zombieTransform = Instantiate(zombiePrefab) as Transform; 
            zombieTransform.position = new Vector3(randomX, 0, 7 + i);
            transform.GetComponent<Rigidbody>().enabled = false;            
            // make it enable when you need and add force to make them fall     
        }
    }