Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

car spawning system like Dashy Crashy

Discussion in 'Scripting' started by NinjaRubberBand, Jan 9, 2016.

  1. NinjaRubberBand

    NinjaRubberBand

    Joined:
    Feb 22, 2013
    Posts:
    243
    Many games including the ios game DasyCrashy has a car spawning system. The game is pretty simple, you control a car, and avoid cars by switching between lanes. Im trying to figure out how to do this car spawning system but i cant really figure it out.
    Does anyone know a place i can buy, or get this system for free?
    Or maybe just show me how to do it? Its the only thing i dont know how i need to do.

    Crashy Dashy gameplay:
     
  2. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    What you're going to have to do is use a random number generator and a prefab. Store the five spawn locations in a range variable, and then pass your random number into the range on instanceation of the prefab.
    You could do this is about 3-5 lines of code.

    Does this help? I could be more detailed if need be.
     
  3. NinjaRubberBand

    NinjaRubberBand

    Joined:
    Feb 22, 2013
    Posts:
    243
    I already done that, but the system needs to be more advanced. For example with this simple system maybe it spawns a car at every spawnlocation and you cant get past them. That is my problem.
     
  4. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    it spawns at Every spawn location. You need to do something like....

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SceneManager : GameManager {
    5.     public float startTime = 3f; //give you a delay at the begining of the game.
    6.     public float timeLeft;
    7.     public float timeBetweenSpawns = 1.5f; //if you want to speed up the spawn rate, use this.
    8.  
    9.     public GameObject[] spawnLocation;
    10.     public GameObject objectToSpawn;
    11.  
    12.     void Awake(){
    13.         spawnLocation = new GameObject[4];// asuming that we only have 5 lanes.
    14.         timeLeft = startTime;
    15.     }
    16.  
    17.     void FixedUpdate(){
    18.         //I think the biggest problem is the lack of a timer, between spawns.
    19.         if(timeLeft > 0){
    20.             timeLeft -= Time.deltaTime;
    21.         }else if(timeLeft <= 0){
    22.             SpawnCar();
    23.             timeLeft =timeBetweenSpawns;
    24.         }
    25.     }
    26.     void SpawnCar(){
    27.         int laneToSpawn = Random.Range (0,spawnLocation.Length);
    28.         Instantiate(objectToSpawn,spawnLocation[laneToSpawn].transform.position,Quaternion.identity);
    29.     }
    30.  
    I did not compile this, but it should work.
     
  5. NinjaRubberBand

    NinjaRubberBand

    Joined:
    Feb 22, 2013
    Posts:
    243
    The script looks very promising, the only thing that doesn't work is something with the objecttospawn variable.. And i cant figure it out..
    NullReferenceException: Object reference not set to an instance of an object
    carSpawner.SpawnCar () (at Assets/carSpawner.js:28)
    carSpawner.FixedUpdate () (at Assets/carSpawner.js:21)

    I also converted it to js, but still says it.