First, create an object that will act as an opponent. Give your opponent the day Enemy.
If the opponent and the figure collide, a tag makes sense because the script can convey that only objects with the enemy tag are relevant as opponents.
Create a C # script called "Enemy".
Open the script in Visual Studio. First the opponent needs one Rigidbody-Component:
private rigidbody2D rigidbody;
Now we need the awake function. It becomes active some time before the start.
void Awake ()
{
Rigidbody = GetComponent ();
}
Now add a Rigidbody2D component in Unity to the opponent.
Now drag the new script onto the opponent.
The movement of the opponent
Now you want the opponent to move in a certain direction based on a speed:
We need: private float speed
public void Move (Vector 2 direction)
{
body.velocity = new Vector2 (direction.x * speed, body.velocity.y);
}
So that the private float spped field appears in the inspector, put in front of it: [SerializeField]
The opponent's direction should change:
We need:
private Vector2 movementDirection;
Now the following is determined:
movementDirection = direction;
You exchange direction.x with movementDirection.x.
You need the update function to change direction:
Move (movementDirection);
After moving in one direction, the opponent returns in the other direction.
The collision with opponents
Furthermore, the opponent must "discover a collision".
private void OnCollisionEnter2D (Collision2D collision)
{
if (collision.gameObject.CompareTag ("Enemy"))
{
movementDirection * = -1f;
}
Enter the following as movementDirection in the awake function:
movementDirection = Vector2.right;
He still needs one Collider. Add a matching 2D collider.
Add a 2nd smaller collider to the top section of the opponent. The 2nd collider should also be a trigger. The collider is designed to eliminate the opponent when the player jumps on it.
Enemies spawning
Right-click in the hierarchy window to create an empty object and name it as a spawner.
The enemies appear depending on the direction of the spawner. Next we need a SpawnerScript.
In it you define the appearance of the opponents.
First we need:
[SerializeField]
Through the SerializeField, the prefabs appeared directly at the spawner.
private enemy [] enemies;
You also need the variable:
[SerializeField]
private float spawnTimeDelay, startSpawnDelay;
With this we set the spawn time and the intervals between spawning.
We also need:
public bool completed;
It sets the final opponent for the spawner. The Game Manager has the "supervision" of the spawners. The completed section specifies that the level is completed when there are no more enemies in the scene and the spawners of all enemies sent into the level.
Now a coroutine is needed for the enemies to spawn.
The opponents should also move in a certain direction.
Finally, it should be checked whether the spawning has been completed.
An IEnumorator is required for the coroutine. Unity handles coroutines in background objects. They also take place in every frame:
private IEnumerator spawn ()
Now the system is told to wait a certain number of seconds for the opponent to appear:
yield return new WaitForSeconds ();
We are waiting for startSpawnDeley:
yield return new WaitForSeconds(startSpawnDelay);
For the time information we need:
for (int i = 0; i <length; i ++)
Each opponent should respawn a certain number after a certain time.
That's why there is also: enemies.Lenght:
for (int i = 0; i <enemies.Lenght; i ++)
Now a reference to the opponent has to be created:
Enemy enemyInstance = Instantiate (enemies [i], transform.position);
Transform.position tells the system that the opponent should appear at the spawner.
Quaternion.identity refers to the rotation.
Enemy enemyInstance = Instantiate (enemies [i], transform.position, Quaternion.identity);
The movement of the opponent can be defined in more detail:
enemyInstance.Move (transform.right);
We add yield return new WaitforSeconds (SpawnTimeDelay); one more time, this time with SpawnTimeDelay.
This means that every time an enemy appears, Unity waits that special time until the next spawning.
Finally, the system needs to check that spawning is complete:
completed = i> = enemies.Lenght - 1;
Finally, the Coroutnie must be started:
void Start ()
{
StartCoroutine (Spawn ());
}
The final script for spawning should look like this:
In the attached spawn script you can now set the spawn frequency:
You can now drag and drop the opponents into the Spawner script: