Untitled

 avatar
unknown
plain_text
4 years ago
1.0 kB
7
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawn : MonoBehaviour
{
    public List<GameObject> cars;
    public Transform[] spawnPoint;
    public bool spawn;
    public float speed;

    private float nextActionTime = 2f;
    public float period = 2f;
    void Start()
    {

    }

    void Update()
    {

        if (spawn)
        {
            if (Time.time > nextActionTime)
            {
                nextActionTime += period;

                CarSpawn();

            }
        }
    }

    public void CarSpawn()
    {
        int randomValue = Random.Range(0, 6);
        int randomValue2 = Random.Range(0, 3);
        print(randomValue);
        GameObject obj = Instantiate(cars[randomValue], spawnPoint[randomValue2].position, Quaternion.Euler(0, -180, 0));
        Rigidbody rb = obj.transform.gameObject.AddComponent<Rigidbody>();
        rb.useGravity = false;
        rb.AddForce(0, 0, -10 * speed);

    }
}
Editor is loading...