unity 按下鼠标左键随机生成物体Instantiate

  • Post author:
  • Post category:其他
using UnityEngine;
using System.Collections;

public class CreateObj : MonoBehaviour {
    public GameObject car;
    public float produceRate = 0.5f;
    private float nextProduce = 0;
    // Update is called once per frame
    void Update()
    {
        //Fire1默认对应摇杆的0号按键(鼠标左键)
        if (Input.GetButton(Fire1) && Time.time > nextProduce)
        {
            nextProduce = Time.time + produceRate;
            //当按下Fire1键时,每0.5秒生成一个汽车.
            Vector3 position = new Vector3(Random.Range(5, 5), 1, Random.Range(5, 5));
            GameObject clone = Instantiate(car, position, car.transform.rotation) as GameObject;
        }
    }

}


版权声明:本文为wf2537原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。