要在脚本中实例化一个游戏对象,可以使用Instantiate函数。以下是一个示例代码,演示如何在特定位置实例化一个物体:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class NewBehaviourScript : MonoBehaviour { public GameObject prefab; // 实例化的预制体 public Transform spawnPoint; // 实例化位置

// Start is called before the first frame update
void Start()
{
    // 在spawnPoint位置实例化prefab
    Instantiate(prefab, spawnPoint.position, spawnPoint.rotation);
}

// Update is called once per frame
void Update()
{
    
}

}

在这个示例中,你需要将要实例化的预制体赋值给prefab变量,并将实例化位置赋值给spawnPoint变量。然后在Start函数中调用Instantiate函数,传入预制体、位置和旋转角度参数,即可在指定位置实例化物体

using SystemCollections;using SystemCollectionsGeneric;using UnityEngine;public class NewBehaviourScript MonoBehaviour public bool canMove = true; 动画组件 private Animator ani; 刚体组件 priva

原文地址: https://www.cveoy.top/t/topic/iauH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录