我是团结的初学者.我想在脚本中实例化gameObject,而不是通过编辑器克隆已经存在的GameObject.当我在unity3d.com中看到下面的代码中的教程时,我很好奇为什么刚体被实例化.
据我所知,刚体在概念上是GameObject的一个组件和GameObject的子组件.即使刚体仅被实例化,游戏对象的实例也会在播放期间显示在场景中.
提前致谢.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)