在固定位置2D游戏中生成

问题描述 投票:0回答:1

我正在尝试做多人游戏,但是在生成预制件时遇到了问题。我希望在2个固定位置生成这些预制件,但我不明白为什么我的脚本不起作用,因为当我开始游戏时,对象会在一个位置生成。我创建了一个空的游戏对象(我将其命名为Spawner并添加了脚本),并添加了2个游戏对象(Position1,Position2)作为Childs。预制件是在Spawner的位置而不是1和2的位置生成的。这是我使用的脚本。还需要向其中添加PhotonView和Photon Transform吗?还有PunRPC?

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

public class SpawnPosition : MonoBehaviour
{
    public GameObject[] powersPrefab;
    public Transform[] points;
    public float beat= (60/130)*2;
    private float timer;




    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (timer > beat)
        {
            GameObject powers = Instantiate(powersPrefab[Random.Range(0, 2)]);
            powers.transform.localPosition = Vector2.zero;
            timer -= beat;

        }
        timer += Time.deltaTime;
    }
}
unity3d photon
1个回答
0
投票

您始终设置

powers.transform.localPosition = Vector2.zero

对象在没有父级的情况下在根级别上实例化,这等同于设置其绝对位置...。您始终将其设置为Unity原点。


您可能想在points中元素的on位置生成它,例如:

var powers = Instantiate(powersPrefab[Random.Range(0, powersPrefab.Length)], points[Random.Range(0, points.Length)], Quaternion.Identity);
© www.soinside.com 2019 - 2024. All rights reserved.