0
最近Unityプロジェクトにいくつか問題がありました。私はチェックポイントを 'x'単位で生成しようとしていましたが、何らかの理由で最初の2つのチェックポイントが正常に生成されますが、その後は何も起こりません。ここでUnity 2D:チェックポイントのチェックポイント生成が正常に動作しませんでした
はCheckpointGeneration.csで私のコードです:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckpointGeneration : MonoBehaviour {
public GameObject checkpointPrefab;
int gap = 5;
List<GameObject> previousCheckpointList;
private void Start()
{
Vector3 targetPos = new Vector3(0, -2.35f);
Instantiate(checkpointPrefab, targetPos,
checkpointPrefab.transform.rotation);
}
private void Update()
{
if (CheckpointController.checkpointsReached != previousCheckpointList && CheckpointController.lastCheckpointReached != null)
{
previousCheckpointList = CheckpointController.checkpointsReached;
Vector3 targetPos = new Vector3(CheckpointController.lastCheckpointReached.transform.position.x + gap, CheckpointController.lastCheckpointReached.transform.position.y);
Instantiate(checkpointPrefab, targetPos, checkpointPrefab.transform.rotation);
}
}
}
これはCheckpointController.csのコードです:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckpointController : MonoBehaviour {
public static List<GameObject> checkpointsReached = new List<GameObject>();
public static GameObject lastCheckpointReached;
GameObject player;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
private void Update()
{
if (checkpointsReached.Count > 0)
{
lastCheckpointReached = checkpointsReached[checkpointsReached.Count - 1];
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
checkpointsReached.Add(gameObject);
GetComponent<SpriteRenderer>().color = new Color(0, 0.86f, 0.29f);
}
Debug.Log("Checkpoint Reached!");
}
public void RespawnAtCheckpoint()
{
if (lastCheckpointReached != null)
{
player.transform.parent.position = lastCheckpointReached.transform.position;
player.GetComponent<PlayerManager>().isDead = false;
Camera.main.transform.position = GameObject.FindGameObjectWithTag("CameraFollow").transform.position;
Debug.Log("Respawning...");
} else
{
GameObject.FindGameObjectWithTag("GameController").GetComponent<ReloadLevel>().Reload();
}
}
}
どれでも答えははるかに高く評価されるだろう!詳しい情報が必要な場合はコメントを残してください。質問を編集して提供します。
シーン内の空のゲームオブジェクトに 'CheckpointGeneration'がアタッチされ、チェックポイントプレハブに' CheckpointController'がありますか? –
@AleksaRisticはい、 'CheckpointGeneration'スクリプトは' GameController'タグが付けられた空のゲームオブジェクトにあり、 'CheckpointController'はチェックポイントプレハブ上にあります。 –
それはgameobjectsとしてゲームを生成しますか? –