• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

31 - C# - 创建Object(物体) - Instantiate();

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:46:37

作用:创建物体Instantiate

应用:开枪,就是点击鼠标,在物体A的某个位置,创建B

代码

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class shoot : MonoBehaviour
  5. {
  6. public GameObject B;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. GameObject a = GameObject.Instantiate(B);//实例化
  15. GameObject b = GameObject.Instantiate(B,transform.position,transform.rotation);
  16. }
  17. }

代码解释

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class shoot : MonoBehaviour
  5. {
  6. public GameObject B;//生成/实例化的东西:B
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. GameObject a = GameObject.Instantiate(B);//实例化
  15. GameObject b = GameObject.Instantiate(B,transform.position,transform.rotation);//实例化,位置在目标的位置,目标的旋转角度
  16. }
  17. }

使用

1.把脚本拉到要生成/实例化东西的A
2.添加要发射的B,一般是已经做好的东西

添加组件

添加Rigibody刚体/C#脚本
a.AddComponent<Rigibody>();