目標
我正在創建一個自上而下的競技場戰斗型別的游戲,我希望你能夠通過按R鍵來重新啟動游戲。
問題是
當我按下R鍵時,整個場景會像它應該的那樣重置,除了所有之前被實體化(然后被摧毀)的敵人會再次生成,而且是一次性的。
代碼
這是enemy spawning代碼:
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
private float nextActionTime = 0.0f;
public float period = 5f;
public GameObject enemy;
void Update()
{
if (Time.time > nextActionTime ) {
nextActionTime = period;
GameObject clone = Instantiate(enemy, new Vector3(-1, 3, 0), Quaternion.ident) 。
clone.tag = "敵人"。
}
}
}
這是玩家代碼,負責重新啟動場景(我已經用破折號標記了我認為相關的部分):
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public GameObject Shield;
public GameObject ShieldInstance;
public float moveSpeed = 4.3f;
public float sheildSpeed = 5f;
Vector2運動。
AudioSource woop;
音頻源waa。
----------------------------
GameObject[] enemies;
----------------------------
bool isDead = false;
void Start() {
woop = GameObject.Find("Main Camera/ShieldSFX").GetComponent<AudioSource>()。
waa = GameObject.Find("Main Camera/DefeatSFX").GetComponent< AudioSource>();
}
void Update()
{
--------------------------------------------------------------
enemies = GameObject.FindGameObjectsWithTag("enemy"/span>)。
--------------------------------------------------------------
movement.x = Input.GetAxisRaw("Horizontal")。
movement.y = Input.GetAxisRaw("Vertical")。
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg -90) 。
if (Input.GetMouseButtonDown(0)
{
if (ShieldInstance != null || transform.GetChild(0).GetComponent<SpriteRenderer>().enable == false) { return; }
woop.Play();
ShieldInstance = Instantiate(Shield, transform.position transform.forward transform.up, transform.rotation)。
ShieldInstance.transform.parent = transform。
}
if (Input.GetMouseButtonUp(0))
{
if (ShieldInstance == null) { return; }
ShieldInstance.transform.parent = null;
ShieldInstance.GetComponent<ShieldController>().LaunchForward(sheildSpeed);
Destroy(ShieldInstance, 2.3f)。
}
-------------------------------------------------------------------------------
if (Input.GetKey("r"/span>)) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex)。
foreach (GameObject one in enemies) {
Destroy(one);
}
}
-------------------------------------------------------------------------------
}
void FixedUpdate() /span> {
if (!isDead) {
rb.MovePosition(rb.position movement * moveSpeed * Time.fixedDeltaTime)。
}
}
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == " enemy") {
waa.Play()。
GameObject.Find("Canvas/gameover").GetComponent<Text>().enabled = true;
transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = false;
GetComponent<PolygonCollider2D>().enabled = false;
}
}
}
而這是敵人的代碼 :
using System;
using System.Collections;
using System.Collections.Generic;
public class EnemyFollow : MonoBehaviour
{
public float moveSpeed。
public ParticleSystem explode;
音頻源的轟鳴聲。
Vector2運動。
GameObject player。
Rigidbody2D rb;
SpriteRenderer sr;
PolygonCollider2D pc;
void Start() {
rb = GetComponent<Rigidbody2D>()。
sr = transform.GetChild(0).GetComponent<SpriteRenderer>()。
pc = GetComponent<PolygonCollider2D>()。
player = GameObject.Find("Player"/span>)。
boom = GameObject.Find("Main Camera/ExplodeSFX").GetComponent<AudioSource>()。
}
void Update()
{
Vector2 difference = (player.transform.position - new Vector3(2, .5f, 0) ) - transform.position。
if (difference.x > 0) {
movement.x = 1;
} else if (difference.x < 0) {
movement.x = -1;
} else {
movement.x = 0;
}
if (difference.y > 0) {
movement.y = 1;
} else if (difference.y < 0) {
movement.y = -1;
} else {
movement.y = 0;
}
rb.MovePosition(rb.position movement * moveSpeed * Time.fixedDeltaTime)。
}
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "shield") {
StartCoroutine(ExplodeF())。
}
}
private IEnumerator ExplodeF() {
explode.Play()。
boom.Play()。
sr.enabled = false;
pc.enabled = false;
yield return new WaitForSeconds(explode。 main.startLifetime.constantMax)。
Destroy(gameObject)。
}
}
我非常感謝任何幫助! 如果你想/需要更多的細節,請留言:)
uj5u.com熱心網友回復:
問題是在Time.time中,它是自應用程式開始的時間,而不是自場景開始的時間。所以如果你在游戲中呆了30秒,Time.time就是30秒。如果你重新加載場景,它仍然是30秒。
你必須計算進入場景后所經過的時間。那么它就不會在場景多載時重生所有的敵人。
uj5u.com熱心網友回復:
當你重啟場景時,除了時間之外,所有的東西都被摧毀并被重置了。
你代碼中的問題是在敵人的產卵器中。 Time.time回傳自你開始游戲(而不是自場景加載)以來的時間。因此,在重啟之后,如果條件為真,那么敵人就會被生成。
如果你想計算自場景加載以來的時間(以秒為單位),你可以做的是在敵人產卵器類中添加一個變數來計算這個時間
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float timeSinceStart = 0;
public float period = 5f;
public GameObject enemy;
void Update()
{
if (timeSinceStart > nextActionTime ) {
nextActionTime = period;
GameObject clone = Instantiate(enemy, new Vector3(-1, 3, 0), Quaternion.ident) 。
clone.tag = "敵人"。
}
timeSinceStart = Time.deltaTime。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329088.html
標籤:
