top down 2d shooter direction of enemies
Hi, I'm doing a 2d top-down shooter the enemies come from above, my problem is this if I sept that the enemies go only straight ahead or only diagonally left or only diagonally right that is a marvel, if instead I do an if or a switch (I created a variable int where I record a random number from 0 to 3 with random.range) it does not work, it goes or just right or just left but jerky, I tried with rigidbody, without rigidbody , with the velocity, with the traslate with the transform, with the moveposition, I tried all but I have not succeeded, I'm going out of my head I can not understand. Why ?
I try to explain well what should happen:
the enemies (they have a rotary shape like the hockey disk to be clear) come from the top screen moving to the down screen, until I give it a direction only then, straight ahead, diagonal left or right diagonal works everything but when I put all three (as per code at random) and if I use the switch or if it does not work anymore. I even tried to create 3 methods but also the same result.
I also tested if there was any problem with the logic but both if the switch worked perfectly, it would seem that there are some problems with the if and the switch (what asssurda) but I no longer think this is one of the countless tests Thank you very much for your help
Vector3 pos = rb.position; int dir = (int)Random.Range(0, 3);
switch (dir)
{
case 0:
pos = new Vector3(pos.x , pos.y - speedEnemy * Time.deltaTime, 0);
rb.MovePosition(transform.position + pos);
break;
case 1:
pos = new Vector3(pos.x - speedEnemy * Time.deltaTime, pos.y - speedEnemy * Time.deltaTime, 0);
rb.MovePosition(transform.position + pos);
break;
case 2:
pos = new Vector3(pos.x + speedEnemy * Time.deltaTime, pos.y - speedEnemy * Time.deltaTime, 0);
rb.MovePosition(transform.position + pos);
break;
}
Your answer
Follow this Question
Related Questions
How do I turn off the previous object? 1 Answer
How can I make my player's gun point at the cursor, not the player itself? 1 Answer
2D Top down player won't move down 2 Answers
2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 0 Answers
My sound effect is not playing 0 Answers