0

目标
所以我正在创建一个自上而下的竞技场战斗类型游戏,我希望你能够通过按 R 重新开始游戏。

问题
当我按下 R 时,整个场景会按原样重置,除了之前实例化(然后被摧毁)的所有敌人都再次生成,所有这些都是一次。

CODE
这是敌人的生成代码:

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.identity);
            clone.tag = "enemy";
        }  
    }
}

这是播放器代码,负责重新启动场景(我用破折号标记了我认为相关的部分):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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 movement;

    AudioSource woop;
    AudioSource 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");
--------------------------------------------------------------
        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>().enabled == 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")) {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            foreach (GameObject one in enemies) {
                Destroy(one);
            }
        }
-------------------------------------------------------------------------------
    }

    void FixedUpdate() {
        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;
using UnityEngine;

public class EnemyFollow : MonoBehaviour
{
    public float moveSpeed;
    public ParticleSystem explode;

    AudioSource boom;

    Vector2 movement;

    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");
        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);
    }
}

我真的很感激任何帮助!如果您想要/需要更多详细信息,请发表评论 :)

4

2 回答 2

2

问题在于Time.time,是应用程序启动后的时间,而不是场景启动后的时间。所以如果你在游戏中 30 秒,Time.time 就是 30 秒。如果你重新加载场景,它仍然是 30 秒。

您必须计算从进入现场以来经过的时间。然后它不会在场景重新加载时重生所有敌人。

于 2021-09-19T07:27:51.230 回答
1

当您重新启动场景时,一切都被破坏并重置,除了时间。

您的代码中的问题在于敌人的生成器。 Time.time返回自您开始游戏以来经过的时间(而不是自加载场景以来)。因此,在重新启动后,if 条件为真,并且生成了敌人。

如果你想计算场景加载后的时间(以秒为单位),你可以做的是在敌人生成器类中添加一个变量来计算时间

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.identity);
            clone.tag = "enemy";
        }  
        timeSinceStart += Time.deltaTime;
    }
}

Time.deltaTime

于 2021-09-19T07:33:45.013 回答