Unity引擎-通过碰撞实例化预制件



我有一个名为Player的游戏对象。播放器附带一个名为Player的脚本组件(相同)在玩家的脚本中,我有一个名为_weaponPremab(游戏对象类型)的字段

在Inspector中,我可以轻松地拖动&将所有预制件从我的"预制件"文件夹中放到_weaponPremab变量中。

到目前为止一切都很好。我想要归档的是:能够基于Collision2D添加我的前言。因此,如果我的玩家与一个前言(比如一把剑)发生冲突,该剑的前言将自动附加并插入玩家脚本内的_weaponPremab字段。

下面是我的播放器脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float _speed = 5.0f;
[SerializeField] private float _fireRate = 0.2f;
private bool _canFire = true;

[SerializeField] private GameObject _weaponPrefab; <- to populate runtime

// Use this for initialization
void Start ()
{
transform.position = Vector3.zero;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space) && _canFire)
StartCoroutine(Shoot());
}

void OnTriggerEnter2D(Collider2D other)
{
//here i don't know how to continue.
}
public IEnumerator Shoot()
{
_canFire = false;
Instantiate(_weaponPrefab, transform.position + new Vector3(0, 1, 0), Quaternion.identity);
yield return new WaitForSeconds(_fireRate);
_canFire = true;
}
}

编辑:我刚刚写了一条评论,说我不知道如何继续。

有很多方法可以实现您的愿望。

我的建议一开始可能超出了你的舒适区,但它将为你提供最大的灵活性,并最终简化游戏的编程/设计/维护(在我看来)。

首先制作一个可脚本化的对象(什么是可脚本化对象,我如何使用它?)

using UnityEngine;
using System.Collections;
using UnityEditor;
public class Item
{
[CreateAssetMenu(menuName = "new Item")]
public static void CreateMyAsset()
{
public GameObject prefab;
// you can add other variables here aswell, like cost, durability etc.
}
}

创建新项目(在Unity中,资产/新项目)

然后创建一个可以容纳项目的单一行为脚本。在你的情况下,让我们命名为"皮卡"。

public class Pickup : MonoBehaviour
{
public Item item;
}

最后,在您的玩家脚本中,将您的on TriggerEnter更改为:

void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponentInChildren<Pickup>())
{
var item = other.GetComponentInChildren<Pickup>().item;
_weaponPrefab = item.prefab;
}
}

当你实例化一个GameObject时,你基本上需要传递3个参数(但不是所有参数都是强制性的,请检查):

  • 预制件
  • 职位
  • 旋转

假设你的角色手中或背后有一个占位符,以便在收集武器后将其保存在那里。这个占位符可以是一个空的游戏对象(没有附加预制件,但会有一个transform.position组件)

现在让我们假设你有一个场景中的武器列表,每个武器都有一个不同的标签。然后你可以做这样的事情:

GameObject weapon;
GameObject placeholder;
public Transform sword;
public Transform bow;
...

void OnTriggerEnter2D(Collider2D other)
{
//You can use a switch/case instead
if(other.gameObject.tag == "sword"){
Instantiate(sword, placeholder.transform.position, Quaternion.identity);
}else if(other.gameObject.tag == "bow"){
Instantiate(bow, placeholder.transform.position, Quaternion.identity);
}...
}

我要做的是首先从resources文件夹将预制件加载到Dictionary<string, GameObject>中。

首先,我用Start方法中的所有武器预制件填充字典,并将对象的标签作为关键字。然后在OnTriggerEnter2D中,我检查标签是否在字典中,然后从那里实例化它。

看看代码:

private Dictionary<string, GameObject> prefabs;
// Use this for initialization
void Start () {
var sword = (GameObject) Resources.Load("Weapons/sword", typeof(GameObject));
prefabs.Add("sword", sword);
// Add other prefabs
};
void OnTriggerEnter2D(Collider2D other)
{
if (prefabs.ContainsKey(other.tag))
Instantiate(prefabs[other.tag]);
}

注意:您的预制件需要在文件夹Assets/Resources/Weapons中,因为我使用了Resources.Load("Weapons/sword", typeof(GameObject));

最新更新