如何使用 Instantiate 在 Player 位置旁边生成对象
How to use Instantiate to spawn objects next to Player position
我正在制作一个 2D 平台游戏,其中一个核心机制是构建平台。我试图使用 Instantiate 在玩家旁边生成平台。
这是我的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class building : MonoBehaviour
{
public GameObject rampUpPrefab;
public GameObject kladkaPrefab;
public Transform Player;
void Start()
{
}
private void Spawn()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Instantiate(rampUpPrefab, Player.transform.position);
}
if (Input.GetKeyDown(KeyCode.W))
{
Instantiate(kladkaPrefab, Player.transform.position);
}
}
private void Instantiate(GameObject kladkaPrefab, Vector3 vector3)
{
}
void Update()
{
Spawn();
}
}
您好,首先我会将您的 Instantiate 函数重命名为其他名称,因为它已经是 UnityEngine.Object.
中存在的函数
这里有两种实例化预制件的基本方法
private void InstantiatePrefab(GameObject prefab, Vector3 position)
{
//Option 1. Spawn the prefab a set position and set rotation.
GameObject.Instantiate(prefab, position, Quaternion.identity);
//Option 2. Spawn the prefab (at prefab position), you can then move it where you want
GameObject instance = Instantiate<GameObject>(prefab);
instance.transform.position = position;
}
他们都做同样的事情。
我正在制作一个 2D 平台游戏,其中一个核心机制是构建平台。我试图使用 Instantiate 在玩家旁边生成平台。
这是我的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class building : MonoBehaviour
{
public GameObject rampUpPrefab;
public GameObject kladkaPrefab;
public Transform Player;
void Start()
{
}
private void Spawn()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Instantiate(rampUpPrefab, Player.transform.position);
}
if (Input.GetKeyDown(KeyCode.W))
{
Instantiate(kladkaPrefab, Player.transform.position);
}
}
private void Instantiate(GameObject kladkaPrefab, Vector3 vector3)
{
}
void Update()
{
Spawn();
}
}
您好,首先我会将您的 Instantiate 函数重命名为其他名称,因为它已经是 UnityEngine.Object.
中存在的函数这里有两种实例化预制件的基本方法
private void InstantiatePrefab(GameObject prefab, Vector3 position)
{
//Option 1. Spawn the prefab a set position and set rotation.
GameObject.Instantiate(prefab, position, Quaternion.identity);
//Option 2. Spawn the prefab (at prefab position), you can then move it where you want
GameObject instance = Instantiate<GameObject>(prefab);
instance.transform.position = position;
}
他们都做同样的事情。