Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- public class PlayerScript : MonoBehaviour {
- private Transform trans;
- public float speed;
- public float size = .5f;
- public float subSize = .5f;
- AudioSource audSource;
- float hor;
- bool lookingRight = true;
- [Header("Sound")]
- public AudioClip eatSFX;
- // Use this for initialization
- void Start () {
- trans = this.gameObject.transform;
- size = trans.localScale.x;
- subSize = trans.localScale.x;
- audSource = GameObject.Find ("Main Camera").GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update () {
- trans.Translate (Vector3.up * speed * Input.GetAxis ("Vertical") * Time.deltaTime);
- trans.Translate (Vector3.right * speed * Input.GetAxis ("Horizontal") * Time.deltaTime);
- trans.localScale = new Vector3 (size, subSize, subSize);
- }
- void FixedUpdate(){
- hor = Input.GetAxis ("Horizontal");
- if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight)) {
- Flip ();
- }
- }
- void OnTriggerEnter2D(Collider2D _other){
- if (_other.gameObject.tag == "Eat") {
- float enemySize = _other.gameObject.GetComponent<EnemyScript> ().size;
- if (enemySize < Mathf.Abs(size)) {
- Destroy (_other.gameObject);
- audSource.PlayOneShot (eatSFX);
- if (size < 0) {
- size -= (enemySize * .5f);
- }
- else if (size > 0) {
- size += (enemySize * .5f);
- }
- subSize += (enemySize * .5f);
- }
- else if (enemySize >= Mathf.Abs(size)) {
- Destroy (this.gameObject);
- SceneManager.LoadScene ("GameOver");
- }
- }
- }
- public void Flip()
- {
- lookingRight = !lookingRight;
- /*Vector3 myScale = transform.localScale;
- myScale.x *= -1;
- transform.localScale = myScale;*/
- size *= -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement