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
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. 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);
}
}
}
CodePudding user response:
Hi you mentioned applying the new movement speed to the 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.
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. 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.
Edit: Also you may need to remove an enemy that gets destroyed from the list or it might have empty entries.
CodePudding user response:
You can just make enemy speed a static field:
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