Instantiate

Tutorial

·

Beginner

·

+0XP

·

5 mins

·

(157)

Unity Technologies

Instantiate

如何在运行期间使用 Instantiate 创建预制件的克隆体。

本教程包含在“初级编程”项目中。

上一教程:

下一教程:数组

Languages available:

1. Instantiate

UsingInstantiate

using UnityEngine;
using System.Collections;

public class UsingInstantiate : MonoBehaviour
{
    public Rigidbody rocketPrefab;
    public Transform barrelEnd;
    
    
    void Update ()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            Rigidbody rocketInstance;
            rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
            rocketInstance.AddForce(barrelEnd.forward * 5000);
        }
    }
}

RocketDestruction

using UnityEngine;
using System.Collections;

public class RocketDestruction : MonoBehaviour
{
    void Start()
    {
        Destroy (gameObject, 1.5f);
    }
}

Complete this tutorial