Introduction to Physics in Unity- Day 9
Objective: Applying behavior to the Enemy game object using Unity’s physics system.
Create a new Cube game object and name it ‘Enemy’ and make it a prefab. Add a red Material to it and leave the ‘Enemy’ game object in the Hierarchy. Create a new script and name it ‘Enemy’. Drag it onto the game object.
To move the enemy down at 4 meters per second, type this in the Update method: transform.Translate(Vector3.down * _speed * Time.deltaTime);
When the enemy is at the bottom of the screen, new enemies are spawned.
If the y position is less than -5f, a new enemy is spawned.
To make the enemies spawn randomly on the x axis, use Unity’s Random.Range property to spawn at a random point between -8 and 8.
To make the code neater, assign a randomX variable to be used for each spawn. Like this: float randomX = Random.Range(-8f, 8f);
transform.position = new Vector3(randomX, 7, 0);