This code is an iOS port of this easy to follow original tutorial & web example. I’m using the phone’s accelerometer to move my ship, instead of the arrow keys on a keyboard, so the main difference is in the Update() function of playerScript.js. There’s also a Boo port on YouTube and a video series based on the original. Don’t forget – Javascript is statically typed in Unity for iOS to improve performance.
playerScript.js
#pragma strict static var LEFT_BOUND:float = -2.9; static var RIGHT_BOUND:float = 2.9; static var playerScore:int; static var playerLives:int; var playerSpeed:int = 0.1; var bullet:Rigidbody; //ref to bulletPrefab in level1 scene var explosion:Transform; //ref to explosionPrefab in level1 scene var angl:int = 0; var timeDiff:float; var delay1:float = 1.5; //two second delay. function Start () { playerScore = 0; playerLives = 3; timeDiff = Time.time + delay1; } function Update () { //set up accelerometer-based movement: var dir: Vector3 = Vector3.zero; var quat: Quaternion = Quaternion.AngleAxis(180, Vector3.forward); // we assume that device is held parallel to the ground // and Home button is in the right hand // remap device acceleration axis to game coordinates: // 1) X plane of the device is mapped onto X plane dir.x = Input.acceleration.x; // clamp acceleration vector to unit sphere, since we only care about direction, not magnitude if (dir.sqrMagnitude > 1) dir.Normalize(); // Move object transform.Translate ( (dir * playerSpeed) * Time.deltaTime); //detect single touch to fire: for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { var tempBullet:Rigidbody; tempBullet = Instantiate( bullet, transform.position, transform.rotation ); } } if (Time.time > timeDiff) //delay by 2 seconds before switching scene { timeDiff = Time.time + delay1; //you won if(playerScore >= 500){ Application.LoadLevel(3); } //you lost if( (playerLives <= 0) ){ Application.LoadLevel(2); } } } function OnGUI() { GUI.Label(Rect(10,50,200,50), "Score: " + playerScore); GUI.Label(Rect(10,80,200,50), "Lives: " + playerLives); } function OnTriggerEnter(otherObject:Collider) { var o:GameObject = otherObject.gameObject; if(o.tag == 'enemy'){ //Enemy's "is Trigger" is checked o.transform.position.y = 7; o.transform.position.x = Random.Range( playerScript.LEFT_BOUND, playerScript.RIGHT_BOUND ); var tempExplosion:Transform; tempExplosion = Instantiate(explosion, transform.position, transform.rotation); playerLives--; } }
bulletScript.js*
#pragma strict var bulletSpeed:int; var explosion:Transform; function Start () { } function Update () { var amtToMove = bulletSpeed * Time.deltaTime; transform.Translate(Vector3.up * amtToMove); if(transform.position.y >= 6.5) { Destroy(gameObject); } } function OnTriggerEnter(otherObject:Collider) { var o = otherObject.gameObject; if(o.tag == 'enemy'){ //Enemy's "is Trigger" is checked playerScript.playerScore += 100; o.transform.position.y = 7; //tell the enemy to reset it's Y off screen o.transform.position.x = Random.Range( playerScript.LEFT_BOUND, playerScript.RIGHT_BOUND ); var tempExplosion:Transform; tempExplosion = Instantiate(explosion, transform.position, transform.rotation); Destroy(gameObject); //destroy self, aka this bullet } }
* NOTE: in more complex games consider using a Ray cast from the gun/ship/object that fires the bullet for a more realistic bullet collision effect.
enemyScript.js
#pragma strict var enemySpeed:int; function Start () { } function Update () { var amtToMove = enemySpeed * Time.deltaTime; transform.Translate( Vector3.down * amtToMove ); if(transform.position.y <= -5) { var rnd = Random.Range( playerScript.LEFT_BOUND, playerScript.RIGHT_BOUND ); transform.position.x = rnd; transform.position.y = 7; } }
mainMenuScript.js
#pragma strict function OnGUI () { var instructionText = 'Instructions\nTilt your phone left or right to move the ship\nTouch screen to fire'; GUI.Label(Rect(10, 50, 270, 200), instructionText); if(GUI.Button(Rect(10, 110, 200, 60), 'START GAME')) { Application.LoadLevel(1); //1 comes from Build Settings list of items in Scenes in Build } }
loseScript.js
#pragma strict function OnGUI () { if( GUI.Button(Rect(10, 50, 300, 50), 'You Lost. Press to play again.')){ Application.LoadLevel(0); } }
winScript.js
#pragma strict function OnGUI () { if( GUI.Button(Rect(10, 50, 300, 50), 'You Won! Press to play again.')){ Application.LoadLevel(0); } }
Pingback: An iOS Port of a Quick Unity3D Asteroids Example, Part 2: Saving Game Settings on Your Device « timshaya