Advertisement
Guest User

Untitled

a guest
Feb 24th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerScript : MonoBehaviour
  5. {
  6.   /// <summary>
  7.   /// 1 - The speed of the ship
  8.   /// </summary>
  9.   public Vector2 speed = new Vector2(50, 50);
  10.  
  11.   // 2 - Store the movement
  12.   private Vector2 movement;
  13.  
  14.   void Update()
  15.   {
  16.     // 3 - Retrieve axis information
  17.     float inputX = Input.GetAxis("Horizontal");
  18.     float inputY = Input.GetAxis("Vertical");
  19.  
  20.     // 4 - Movement per direction
  21.     movement = new Vector2(
  22.       speed.x * inputX,
  23.       speed.y * inputY);
  24.  
  25.          // 5 - Shooting
  26.     bool shoot = Input.GetButtonDown("Fire1");
  27.     shoot |= Input.GetButtonDown("Fire2");
  28.     // Careful: For Mac users, ctrl + arrow is a bad idea
  29.  
  30.     if (shoot)
  31.     {
  32.       WeaponScript weapon = GetComponent<WeaponScript>();
  33.       if (weapon != null)
  34.       {
  35.         // false because the player is not an enemy
  36.         weapon.Attack(false);
  37.       }
  38.     }
  39.     // 6 - Make sure we are not outside the camera bounds
  40.     var dist = (transform.position - Camera.main.transform.position).z;
  41.  
  42.     var leftBorder = Camera.main.ViewportToWorldPoint(
  43.       new Vector3(0, 0, dist)
  44.     ).x;
  45.  
  46.     var rightBorder = Camera.main.ViewportToWorldPoint(
  47.       new Vector3(1, 0, dist)
  48.     ).x;
  49.  
  50.     var topBorder = Camera.main.ViewportToWorldPoint(
  51.       new Vector3(0, 0, dist)
  52.     ).y;
  53.  
  54.     var bottomBorder = Camera.main.ViewportToWorldPoint(
  55.       new Vector3(0, 1, dist)
  56.     ).y;
  57.  
  58.     transform.position = new Vector3(
  59.       Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
  60.       Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
  61.       transform.position.z
  62.     );
  63.  
  64.     // End of the update method
  65.   }
  66.  
  67.  
  68.   void FixedUpdate()
  69.   {
  70.     // 5 - Move the game object
  71.     rigidbody2D.velocity = movement;
  72.   }
  73.  
  74.   void OnCollisionEnter2D(Collision2D collision)
  75.   {
  76.     bool damagePlayer = false;
  77.  
  78.     // Collision with enemy
  79.     EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript>();
  80.     if (enemy != null)
  81.     {
  82.       // Kill the enemy
  83.       HealthScript enemyHealth = enemy.GetComponent<HealthScript>();
  84.       if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp);
  85.  
  86.       damagePlayer = true;
  87.     }
  88.  
  89.     // Damage the player
  90.     if (damagePlayer)
  91.     {
  92.       HealthScript playerHealth = this.GetComponent<HealthScript>();
  93.       if (playerHealth != null) playerHealth.Damage(1);
  94.     }
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement