Will someone help me with making better code that makes the enemies less predictable and harder to shoot in my space invaders game?
void Update()
{
if (transform.position.x >= 6)
{
transform.position = new Vector2(transform.position.x - 1, transform.position.y - 1);
speed = -speed;
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
else if (transform.position.x <= -6)
{
transform.position = new Vector2(transform.position.x + 1, transform.position.y - 1);
speed = -speed;
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
}
Right now it just very easy to stay in the middle of the screen and not move and just shoot at the incoming enemies and they wont make it anywhere near you.
Answer by Lukas-Barbarossa · Mar 20, 2020 at 12:52 PM
Hi @glharwell
You can use Random.Range to make the movement less predictable and even to have random spawn points
Random.Range returns a float between the minimum and the maximum you pass in as arguments. So if you wanted movement in the x and y to be around -1 but not exactly (say between -0.8 and -1.2) you can get that from the number it returns.
Something like
transform.position = new Vector2(transform.position.x - Random.range(-0.8, -1.2), transform.position.y - Random.range(-0.8, -1.2);
Of course, the way you have your code set up, this would happen in Update() which means you would get an anemy that is going faster and slower on every frame, and it is also fairly heavy to calculate 2 random numbers on every frame for something this simple.
Instead you might want to say, "I want a random movement, but I want the enemies to stick to that random movement until they die, in which case you would calculate the Random.Range outside the Update(), store it in a variable and use that instead of your -1.
You could also use Random.Range to randomise your spawnpoints. For example, if you created 5 empty game objects as spawn points you could get a random number and Instantiate your enemy there instead of in the centre of the screen. Something like
public Transform spawnPoint1; //drag in the relevant empty game object here
public Transform spawnPoint2; //drag in the relevant empty game object here
public Transform spawnPoint3; //drag in the relevant empty game object here
public Transform spawnPoint4; //drag in the relevant empty game object here
public Transform spawnPoint5; //drag in the relevant empty game object here
public GameObject enemy; //drag in your enemy prefab here
void Start() //or whenever you want this to happen
{
//Random.Range as an int is inclusive of the minimum, but not inclusive of the maximum, so this will give you either 0, 1, 2, 3, 4
int rand = Random.Range(0, 5);
if(rand == 0)
{
//Instantiate the enemy at spawnpoint1
Instantiate(enemy, spawnPoint1.position, spawnPoint1.rotation);
}
else if (rand == 1)
{
//Instantiate the enemy at spawnpoint2
Instantiate(enemy, spawnPoint2.position, spawnPoint2.rotation);
}
else if (rand == 2)
{
//Instantiate the enemy at spawnpoint3
Instantiate(enemy, spawnPoint3.position, spawnPoint3.rotation);
}
else if (rand == 3)
{
//Instantiate the enemy at spawnpoint4
Instantiate(enemy, spawnPoint4.position, spawnPoint4.rotation);
}
else if (rand == 4)
{
//Instantiate the enemy at spawnpoint5
Instantiate(enemy, spawnPoint5.position, spawnPoint5.rotation);
}
}
You could also use this Random game object idea to move your enemies to a random target game object, rather than how you are currently moving, so they would pick an empty game object to move towards based on a random number!
Hope that helps. Hit reply if you struggle with this and I'll try to help you get it working
Your answer
Follow this Question
Related Questions
2D shader / lighting like Terraria or Starbound 2 Answers
Problem with raycasting towards mouse 0 Answers
Corgi Engine - Character glitches with "jump" animation on top of a ladder 0 Answers
After Rotating a Prefab, Transform.Position of children is inaccurate 1 Answer
Pipe Game Water Flow 0 Answers