79
There is going to be a number of scripts in this scene, therefore I am going to reveal them in a specific order that delays the scripts which have dependencies. So I am going to begin with independent scripts that do not need to reference other scripts. Therefore I am going to leave the shuttle for a while to discuss the targets that our shuttle will be shooting. We are going to use prefabs to create targets, since we are going to have a relatively large number of targets in the scene, and it would be great to be able to handle these objects from single place.
To create the target prefab, add a cube to the scene. We are going to add the script Target to this cube, which has only one variable called hit. hit is a boolean value that determines whether the target has already been hit or not. The script Target is shown in Listing 15.
1. using UnityEngine;
2. using System.Collections; 3.
4. public class Target : MonoBehaviour { 5.
6. //The bullet sets this value to true when it hits the target 7. public bool hit = false;
8.
9. //Set this flag to true after calling destroy 10. bool destroyed = false;
11. 12. void Start () { 13. 14. } 15. 16. void Update () { 17. if(hit){
18. //The bullet has hit the target. Play destruction 19. //animation, which is rotation and size reduction 20. transform.Rotate(0, 720 * Time.deltaTime, 0);
21. transform.localScale -= Vector3.one * Time.deltaTime; 22.
23. //If we have not called Destroy() yet, call it now 24. if(!destroyed){
25. //Delay destruction for one second so the player
26. //can see the animation
27. Destroy(gameObject, 1);
28. //Set the destruction flag to prevent multiple
29. //Destroy() calls 30. destroyed = true; 31. } 32. } 33. } 34. }
80
This script checks the value of hit, and eventually destroys the object if the value is true. Since there are no statement in this code that changes the value of hit, the object maintains its state until another script modifies this value. We are going to see shortly how does the bullet check for a collision between itself and the object, and change the value of hit if such collision exists. The script also adds a sort of animation to the object, by rotating it and reducing its size with time once the object is hit. This animation makes the target looks like if it is falling down.
Rotation is performed using the function transform.Rotate(), which we have dealt with in many cases before. On the other hand, reducing the object size is achieved by subtracting a small amount from the object scale. This amount is equal to a vector that has components with values equal to Time.deltaTime, which makes size reduction in a speed of one meter per second; so the object disappears after one second because of zero scale. Therefore, we call Destroy() in line 27 and pass to it the object we want to destroy (in this case it is the same target object, which can be achieved through the variable gameObject). In addition to the object, we also pass to Destroy() the time it should wait before performing the destruction. In this case the time is one second.
What we mean by destroying the object in this context is removing the object completely from the scene, and this can be observed by disappearance of the object from the hierarchy. To avoid calling Destroy()
more than one time, we used the variable destroyed as a flag for calling that function. We need destroyed
in this case because we have delayed the destruction to show the animation. This delay will cause Update()
to be called several times during next second before the object is actually destroyed. During this second, we want to repeatedly call falling animation, but we want to call Destroy() only once.
We well now add another script to the target game object. The job of this script is to move the target so it does not stay still. This movement depends on time only and not on the player input like we have done several times before. The reason is obvious: the player do not control the targets, they move by themselves. The script is going to move the target in a specific direction with a constant speed. Listing 16 shows AutoMover script, which moves the object over the time in the direction specified by the speed
vector. When the object leaves the field of view of the camera from one side, it should be repositioned in the opposite side. This movement is known as wrapping, and is common in many games, including the famous classic game Pac-Man. Listing 17 shows Wrapper script, which rotates the object around the scene based on its position on x and z axes.
81 1. using UnityEngine;
2. using System.Collections; 3.
4. public class AutoMover : MonoBehaviour { 5.
6. //Movement speed
7. public Vector3 speed = new Vector3(0, 0, 0); 8. 9. void Start () { 10. 11. } 12. 13. void Update () {
14. //Move the object with the specified speed 15. transform.Translate(speed * Time.deltaTime); 16. }
17. }
Listing 16: A script that moves the object in a specific direction with a constant speed
Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more Click on the ad to read more
Maersk.com/Mitas
�e Graduate Programme for Engineers and Geoscientists