Running on platforms is not enough for the prototype; we need to provide a player with a goal. Let's say that our prototype level is complete once the player has gathered a number of collectibles that we randomly scatter in the level.
To achieve that, we add a few lines to the PlatManager
script and create a new prefab to be instantiated as our collectible game object.
Open the PlatManager
script in Monodevelop and be ready to add the lines described here.
public Transform collectPref; private Transform collectible;
TossCollectible()
that casts a random result to decide whether to instantiate a collectible on the next platform to be created in the level. Add the following lines to the script:void TossCollectible(){ float f = Random.Range(0f,1f); Debug.Log(f); if (f > 0.5){ Vector3 v = new Vector3 (nextPos.x, nextPos.y + delta, 0); collectible = (Transform)Instantiate(collectPref, v, Quaternion.identity); } }
TossCollectible()
when we instantiate a new platform in the level. What follows is the complete updated FixedUpdate()
function, with the new lines highlighted:void FixedUpdate(){ UpdatePos(); if(actualPlat != null && charX > actualPlat.transform.position.x + delta){float strict = actualPlat.transform.position.x; prevPlat = actualPlat; actualPlat = null; SetScalesAndGaps(); nextPos = new Vector3(strict + platScale.x + gap, yGap, charZ); nextPlat = (Transform)Instantiate(platBrick, nextPos, Quaternion.identity ); nextPlat.localScale = platScale; TossCollectible(); } if(actualPlat != null && charX < actualPlat.transform.position.x - delta){ float strict = actualPlat.transform.position.x; prevPlat = actualPlat; actualPlat = null; SetScalesAndGaps(); nextPos = new Vector3(strict - platScale.x - gap, yGap, charZ); nextPlat = (Transform)Instantiate(platBrick, nextPos, Quaternion.identity ); nextPlat.localScale = platScale; TossCollectible(); } if(prevPlat != null){ float strict = prevPlat.transform.position.x; if(charX > strict - delta && charX < strict + delta){ Destroy(nextPlat.gameObject); nextPlat = null; actualPlat = prevPlat; prevPlat = null; } } if(nextPlat != null){ float strict = nextPlat.transform.position.x; if(charX > strict - delta && charX < strict + delta){ actualPlat = nextPlat; nextPlat = null; Destroy(prevPlat.gameObject); } } }
Runner
script to manage the collisions between the character and the collectibles. Open the script in Monodevelop. We begin with declaring a new public int
variable to store the number of items collected so far. We will display this data in the GUI, later. Add the following declaration at the top of the script:public int collected;
Start()
function, we initialize collected
to 0
, with one simple instruction. To make things more clear, we add the full Start()
function as follows:void Start () { charAnimator = GameObject.Find("runner").GetComponent<Animator>(); horAcceleration = 4f; bIsTouch = false; jumpVel = new Vector3 (0,10,0); collected = 0; }
OnCollisionEnter()
function by adding a check for a second tag, named collectible, that we will create pretty soon. If we get a positive check with the tag, we can destroy the collectible instance and add 1
to the number of collected items. What follows is the updated OnCollisionEnter()
function:void OnCollisionEnter(Collision c){ if(c.gameObject.tag == "platform"){ bIsTouch = true; charAnimator.SetBool("bJump",false); } if(c.gameObject.tag == "collectible"){ Destroy(c.gameObject); collected += 1; } }
.35
on all axes and be sure that its position is reset to 0
on all axes.Prefabs
folder of your project and name it coll_prefab. Then drag the sphere onto the prefab, as shown in the following screenshot:The collectible objects are spawned with a random chance on platforms that get instantiated at runtime. Upon collision with the game character, they get destroyed and their count is increased by one.
In the previous script, we used the OnCollisionEnter()
function to detect when the character hits a collectible. Unity offers another method called OnTriggerEnter()
that detects when two objects collide without generating a collision. The OnTriggerEnter()
function is useful when you don't want two colliding objects to physically react upon collision. You can check out this link for a description of the difference between OnCollisionEnter()
and OnTriggerEnter()
: http://answers.unity3d.com/questions/790724/what-is-the-difference-between-oncollisionenter-an.html.
To complete our working prototype we need to add a control for the game camera so it follows the character. There is already a camera in the scene called Main Camera, so we will take advantage of it.
3.137.167.195