In my top down 2d game I created an int variable called dir
I assigned him a random number between 0 and 3
I made a switch case where if it is 0 the enemy goes straight if 1 the enemy goes diagonally left if it is 2 the enemy goes right diagonal
this is the part of the code
but it does not work
// move the enemy
Vector2 pos = transform.position;
Vector2 velocity;
int dir = (int)Random.Range(0, 3);
Debug.Log(dir);
switch (dir)
{
case 0:
velocity = new Vector2(0, -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("DRITTO");
break;
case 1:
velocity = new Vector2(-speedEnemy * Time.deltaTime, -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("SINISTRA");
break;
case 2:
velocity = new Vector2(speedEnemy * Time.deltaTime, -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("DESTRA");
break;
}
though the switch case works regularly the enemy always goes left diagonally and not with a smooth movement
my goal is to create random the direction of the enemies
because? what am I doing wrong?
Many thanks for your help
You have to Update the position based upon current position of Object. So your code will become.
Vector2 pos = transform.position;
Vector2 velocity;
int dir = (int)Random.Range(0, 3);
Debug.Log(dir);
switch (dir)
{
case 0:
// Use current value as starting
velocity = new Vector2( this.transform.position.x + 0, this.transform.position.y -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("DRITTO");
break;
case 1:
velocity = new Vector2(this.transform.position.x-speedEnemy * Time.deltaTime, this.transform.position.y -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("SINISTRA");
break;
case 2:
velocity = new Vector2( this.transform.position.x+speedEnemy * Time.deltaTime, this.transform.position.y -speedEnemy * Time.deltaTime);
this.transform.Translate(velocity, Space.World);
Debug.Log("DESTRA");
break;
}
hi @dev-waqas,
many tanks i try but now thr code give the follow error
transform.position assign attempt for ‘cell_white(Clone)’ is not valid. Input position is { 153413570376001570000000000000000000000.000000, Infinity, 0.000000 }.
UnityEngine.Transform:Translate(Vector3, Space)
EnemyControl:Update() (at Assets/Scripts/EnemyControl.cs:40)
I have 4 enemy cell_white, cell_red,cell_black,cell_yellow
this is the code that i use for eneny spawn random
public GameObject[] EnemiesPrefab;
float maxSpawnRateInSecond = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void SpawnEnemy()
{
// Botton left point of the screen
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
// Top right point of the screen
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
int randEnemy;
randEnemy = Random.Range(0, EnemiesPrefab.Length);
// Instantiate an enemy
GameObject anEnemy = (GameObject)Instantiate(EnemiesPrefab[randEnemy]);
anEnemy.transform.position = new Vector2(Random.Range(min.x, max.x), max.y);
// Schedule when spawn next enemy
ScheduleNextEnemySpawn();
}
// Schedule when spawn next enemy
void ScheduleNextEnemySpawn()
{
float spawnInSeconds;
if (maxSpawnRateInSecond > 1f)
{
// Pixk a number between 1 and maxSpawnRateInSecond
spawnInSeconds = Random.Range(1f, maxSpawnRateInSecond);
}
else
spawnInSeconds = 1f;
Invoke("SpawnEnemy", spawnInSeconds);
}
// Method to increase the difficult of the game
void IncreaseSpawnRate()
{
if (maxSpawnRateInSecond > 1f)
maxSpawnRateInSecond--;
if (maxSpawnRateInSecond == 1f)
CancelInvoke("IncreaseSpawnRate");
}
// Method to start enemy spawner
public void ScheduleEnemySpawner()
{
Invoke("SpawnEnemy", maxSpawnRateInSecond);
// Increase spawn rate every 30 seconds
InvokeRepeating("IncreaseSpawnRate", 0f, 30f);
}
// Method to stop enemy spawner
public void UnscheduleEnemySpawner()
{
CancelInvoke("SpawnEnemy");
CancelInvoke("IncreaseSpawnRate");
}
many thanks