Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. public class PlayerScript : MonoBehaviour {
  5.  
  6.     private Transform trans;
  7.     public float speed;
  8.     public float size = .5f;
  9.     public float subSize = .5f;
  10.     AudioSource audSource;
  11.     float hor;
  12.     bool lookingRight = true;
  13.     [Header("Sound")]
  14.     public AudioClip eatSFX;
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.         trans = this.gameObject.transform;
  19.         size = trans.localScale.x;
  20.         subSize = trans.localScale.x;
  21.         audSource = GameObject.Find ("Main Camera").GetComponent<AudioSource>();
  22.     }
  23.    
  24.     // Update is called once per frame
  25.     void Update () {
  26.         trans.Translate (Vector3.up * speed * Input.GetAxis ("Vertical") * Time.deltaTime);
  27.         trans.Translate (Vector3.right * speed * Input.GetAxis ("Horizontal") * Time.deltaTime);
  28.         trans.localScale = new Vector3 (size, subSize, subSize);
  29.     }
  30.  
  31.     void FixedUpdate(){
  32.         hor = Input.GetAxis ("Horizontal");
  33.         if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight)) {
  34.             Flip ();
  35.         }
  36.     }
  37.  
  38.     void OnTriggerEnter2D(Collider2D _other){
  39.         if (_other.gameObject.tag == "Eat") {
  40.             float enemySize = _other.gameObject.GetComponent<EnemyScript> ().size;
  41.             if (enemySize < Mathf.Abs(size)) {
  42.                 Destroy (_other.gameObject);
  43.                 audSource.PlayOneShot (eatSFX);
  44.                 if (size < 0) {
  45.                     size -= (enemySize * .5f);
  46.                 }
  47.                 else if (size > 0) {
  48.                     size += (enemySize * .5f);
  49.                 }
  50.                 subSize += (enemySize * .5f);
  51.             }
  52.             else if (enemySize >= Mathf.Abs(size)) {
  53.                 Destroy (this.gameObject);
  54.                 SceneManager.LoadScene ("GameOver");
  55.             }
  56.         }
  57.     }
  58.  
  59.     public void Flip()
  60.     {
  61.         lookingRight = !lookingRight;
  62.         /*Vector3 myScale = transform.localScale;
  63.         myScale.x *= -1;
  64.         transform.localScale = myScale;*/
  65.         size *= -1;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement