Unity 2D:检查点上的检查点生成无法正常工作
Unity 2D: Checkpoint generation on checkpoint reached not working properly
最近我的 Unity 项目遇到了一些问题。我一直在尝试每 'x' 个单位生成检查点,但是,由于某种原因,前两个检查点成功生成,但之后没有任何反应。
这是我在 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();
}
}
}
任何答案将不胜感激!如果您需要更多信息,请发表评论,我将编辑问题以提供它。
我对您的代码进行了更改,以便您了解更简洁的代码。如果您不理解代码中的某些内容,请问我。我还为全局变量做了另一个 class 。考虑使用它,因为按名称查找游戏对象并不好。
public class GlobalClass : MonoBehaviour
{
//static objects are not seen in inspector so we will asign player to public gameobject and inside script we transfer it to static gameobject so now we can get player gameobject with GlobalClass.player
public static GameObject player;
public GameObject _player;
//With this you can set all other gameobjects like your GameController or something like that
private void Start()
{
player = _player;
}
}
public class CheckpointGeneration : MonoBehaviour
{
public GameObject checkpointPrefab;
public float gap = 5.0f;
public static int spawnedCheckpoints = 0; //You can alse set it as bool but with this you can maybe make to have 2 or more checkpoints
private static Vector2 lastCheckpointPos = new Vector2(0, -2.35f);
private void Start()
{
Instantiate(checkpointPrefab, lastCheckpointPos, checkpointPrefab.transform.rotation);
spawnedCheckpoints++;
}
private void Update()
{
if (spawnedCheckpoints < 1)
{
SpawnCheckpoint();
}
}
public static void RespawnAtCheckpoint()
{
if (CheckpointGeneration.spawnedCheckpoints > 0)
{
GlobalClass.player.transform.parent.position = CheckpointGeneration.lastCheckpointPos;
GlobalClass.player.GetComponent<PlayerManager>().isDead = false;
Camera.main.transform.position = GameObject.FindGameObjectWithTag("CameraFollow").transform.position;
Debug.Log("Respawning...");
} else
{
GameObject.FindGameObjectWithTag("GameController").GetComponent<ReloadLevel>().Reload();
}
} //Call this method when player die
private static void SpawnCheckpoint()
{
Vector2 newCheckpointPos = new Vector2(lastCheckpointPos.x + gap, lastCheckpointPos.y);
Instantiate(checkpointPrefab, newCheckpointPos, checkpointPrefab.transform.rotation); //Consider using Quaternion.identity
lastCheckpointPos = newCheckpointPos;
spawnedCheckpoints++;
}
}
public class CheckpointController : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
CheckpointGeneration.spawnedCheckpoints--; //with this we sent message to your CheckpointGeneration script that this checkpoint is reached
Debug.Log("Checkpoint Reached!");
Destroy(gameObject);//When checkpoint is reached we destroy it and will spawn new one
}
}
}
最近我的 Unity 项目遇到了一些问题。我一直在尝试每 'x' 个单位生成检查点,但是,由于某种原因,前两个检查点成功生成,但之后没有任何反应。
这是我在 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();
}
}
}
任何答案将不胜感激!如果您需要更多信息,请发表评论,我将编辑问题以提供它。
我对您的代码进行了更改,以便您了解更简洁的代码。如果您不理解代码中的某些内容,请问我。我还为全局变量做了另一个 class 。考虑使用它,因为按名称查找游戏对象并不好。
public class GlobalClass : MonoBehaviour
{
//static objects are not seen in inspector so we will asign player to public gameobject and inside script we transfer it to static gameobject so now we can get player gameobject with GlobalClass.player
public static GameObject player;
public GameObject _player;
//With this you can set all other gameobjects like your GameController or something like that
private void Start()
{
player = _player;
}
}
public class CheckpointGeneration : MonoBehaviour
{
public GameObject checkpointPrefab;
public float gap = 5.0f;
public static int spawnedCheckpoints = 0; //You can alse set it as bool but with this you can maybe make to have 2 or more checkpoints
private static Vector2 lastCheckpointPos = new Vector2(0, -2.35f);
private void Start()
{
Instantiate(checkpointPrefab, lastCheckpointPos, checkpointPrefab.transform.rotation);
spawnedCheckpoints++;
}
private void Update()
{
if (spawnedCheckpoints < 1)
{
SpawnCheckpoint();
}
}
public static void RespawnAtCheckpoint()
{
if (CheckpointGeneration.spawnedCheckpoints > 0)
{
GlobalClass.player.transform.parent.position = CheckpointGeneration.lastCheckpointPos;
GlobalClass.player.GetComponent<PlayerManager>().isDead = false;
Camera.main.transform.position = GameObject.FindGameObjectWithTag("CameraFollow").transform.position;
Debug.Log("Respawning...");
} else
{
GameObject.FindGameObjectWithTag("GameController").GetComponent<ReloadLevel>().Reload();
}
} //Call this method when player die
private static void SpawnCheckpoint()
{
Vector2 newCheckpointPos = new Vector2(lastCheckpointPos.x + gap, lastCheckpointPos.y);
Instantiate(checkpointPrefab, newCheckpointPos, checkpointPrefab.transform.rotation); //Consider using Quaternion.identity
lastCheckpointPos = newCheckpointPos;
spawnedCheckpoints++;
}
}
public class CheckpointController : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
CheckpointGeneration.spawnedCheckpoints--; //with this we sent message to your CheckpointGeneration script that this checkpoint is reached
Debug.Log("Checkpoint Reached!");
Destroy(gameObject);//When checkpoint is reached we destroy it and will spawn new one
}
}
}