简体   繁体   English

如何提高敌人的速度?

[英]How to do I increase the speed of enemies?

I tried the following code but the speed boost only applied to the newly generating enemies.I have made it so that the player itself dose'nt move but the enemies do.我尝试了以下代码,但速度提升仅适用于新生成的敌人。我这样做是为了让玩家本身不会移动,但敌人会移动。

The generator of the enemies I used the spawnpoints array敌人的生成器我使用了 spawnpoints 数组

using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEngine;


public class Generator : MonoBehaviour
{
    public Transform[] EnemySpawns;
    public GameObject[] EnemyPrefab;
    public float timeLeft = 1.0f;
    public float usesamevarasabove = 1.0f;
    void Update()
    {
        timeLeft -= Time.deltaTime;
        if (timeLeft <= 0)
        {
            int RandEnemy = Random.Range(0, EnemyPrefab.Length);
            int RandSpawPoint = Random.Range(0, EnemySpawns.Length);
            Instantiate(EnemyPrefab[0], EnemySpawns[RandSpawPoint].position, transform.rotation);
            timeLeft = usesamevarasabove;
        }
    }
}

The curse there will be 6 curses, 3 good 3 bad in the bad curse i want to increase the speed at which the enemies move but when the player and the curse collide the already existing enemies dont change but the newly generated ones have higher speed.诅咒将有 6 个诅咒,3 个好 3 个坏的坏诅咒我想提高敌人移动的速度,但是当玩家和诅咒碰撞时,已经存在的敌人不会改变,但新生成的敌人速度更快。 I am applying the speed change to the enemy prefab but it still dosent apply to我正在将速度变化应用到敌人的预制件上,但它仍然不适用

int curse = Random.Range(1,1);
        Debug.Log(curse);
        if(curse == 0)
        {
            
        }
        else if(curse == 1)
        {
            em = e.GetComponent<EnemyMovement>();
            em.speed = new Vector2(10, 0);
            
        }

the enemy speed敌人速度

using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
    public Vector2 speed = new Vector2(5, 0);
    private Vector2 screenBounds;
    void Start()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }
    void Update()
    {
        Vector3 movement = new Vector3(speed.x * -1, 0, 0);
        movement *= Time.deltaTime;
        transform.Translate(movement);
        if(transform.position.x < -20){
            Destroy(this.gameObject);
        }
    }
}

Hi you mentioned applying the new movement speed to the Enemy Prefab.您好,您提到过将新的移动速度应用于 Enemy Prefab。 This will indeed cause all of the new objects spawned to have the increased movement speed.这确实会导致生成的所有新对象都具有更高的移动速度。

However all of the previously spawned enemies have their own instances of the EnemyMovement script and thus will still have the previous Speed.然而,之前生成的所有敌人都有自己的 EnemyMovement 脚本实例,因此仍将具有之前的速度。

In the generator you will need to make sure to keep a List of all the previously spawned Enemies and update those when the player collides with the curse.在生成器中,您需要确保保留所有先前生成的敌人的列表,并在玩家与诅咒发生碰撞时更新这些敌人。

using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEngine;

public class Generator : MonoBehaviour
{
    public Transform[] EnemySpawns;
    public GameObject[] EnemyPrefab;
    public List<GameObject> spawnedEnemies = new List<GameObject>();
    public float timeLeft = 1.0f;
    public float usesamevarasabove = 1.0f;

    void Update()
    {
        timeLeft -= Time.deltaTime;
        if (timeLeft <= 0)
        {
            int RandEnemy = Random.Range(0, EnemyPrefab.Length);
            int RandSpawPoint = Random.Range(0, EnemySpawns.Length);
            GameObject tempGO = Instantiate(EnemyPrefab[0], EnemySpawns[RandSpawPoint].position,         transform.rotation);
            spawnedEnemies.Add(tempGO);
            timeLeft = usesamevarasabove;
        }
    }

    public void UpdateEnemySpeed(Vector2 newSpeed) {
        for(int i = 0; i < spawnedEnemies.Count; i++) {
             spawnedEnemies[i].GetComponent<EnemyMovement>().speed = newSpeed;
        }
    }
}

I added a List that keeps track of all the spawned enemies to your Generator script and added a function that loops through all these spawned objects and updates the speed in all of them.我在生成器脚本中添加了一个跟踪所有生成的敌人的列表,并添加了一个 function 循环遍历所有这些生成的对象并更新所有对象的速度。 When the player collides with the curse just make it call the UpdateEnemySpeed function with the new speed and it should update all the previously spawned enemies.当玩家与诅咒发生碰撞时,只需让它以新速度调用 UpdateEnemySpeed function,它应该会更新所有先前生成的敌人。

Edit: Also you may need to remove an enemy that gets destroyed from the list or it might have empty entries.编辑:您还可能需要从列表中删除一个被摧毁的敌人,或者它可能有空条目。

You can just make enemy speed a static field:您可以将敌人速度设置为 static 字段:

using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
    public static Vector2 speed = new Vector2(5, 0);
    private Vector2 screenBounds;
    void Start()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }
    void Update()
    {
        Vector3 movement = new Vector3(speed.x * -1, 0, 0);
        movement *= Time.deltaTime;
        transform.Translate(movement);
        if(transform.position.x < -20){
            Destroy(this.gameObject);
        }
    }
}

This way change should be applied to all objects that are using EnemyMovement script, because static field is shared by all instances of a class这种方式的更改应该应用于所有使用 EnemyMovement 脚本的对象,因为 static 字段由 class 的所有实例共享

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM