如何提高敌人的速度?



我尝试了以下代码,但速度提升只适用于新生成的敌人。我这样做是为了让玩家自己不会移动,但敌人会移动。

敌人的生成器我使用了刷出点数组

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

诅咒会有6个诅咒,3个好诅咒3个坏诅咒在坏诅咒中,我想提高敌人移动的速度但是当玩家和诅咒发生碰撞时,已经存在的敌人不会改变,但新生成的敌人速度更快。我正在把速度改变应用到敌人的预制件上,但它仍然不适用于

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

}

敌人速度

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

你好,你提到将新的移动速度应用于敌人预制。这确实会导致所有新生成的物体的移动速度增加。

然而,所有之前生成的敌人都有自己的EnemyMovement脚本实例,因此仍然具有之前的Speed。

在生成器中,您需要确保保留所有先前生成的敌人的列表,并在玩家与诅咒碰撞时更新这些列表。

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

我在你的Generator脚本中添加了一个跟踪所有生成敌人的列表,并添加了一个循环遍历所有这些生成对象并更新它们的速度的函数。当玩家与诅咒发生碰撞时,只要让它以新的速度调用UpdateEnemySpeed函数,它就会更新之前生成的所有敌人。

编辑:此外,你可能需要从列表中删除被摧毁的敌人,或者它可能有空条目。

你可以将敌人速度设置为静态场:

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

这种方式的改变应该应用于使用EnemyMovement脚本的所有对象,因为静态字段是由类的所有实例共享的

最新更新