Advertisement
Guest User

PlayerController for Magnetize

a guest
Mar 30th, 2020
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9.     private Rigidbody2D rb2D;
  10.     public float moveSpeed = 5f;
  11.     public float pullForce = 100f;
  12.     public float rotateSpeed = 360f;
  13.     private GameObject closestTower;
  14.     private GameObject hookedTower;
  15.     private bool isPulled = false;
  16.     private UIControllerScript uiControl;
  17.     private AudioSource myAudio;
  18.     private bool isCrashed = false;
  19.     public Transform transformPosition;
  20.     private Vector3 startPosition;
  21.  
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {
  25.         rb2D = this.gameObject.GetComponent<Rigidbody2D>();
  26.         myAudio = this.gameObject.GetComponent<AudioSource>();
  27.         uiControl = GameObject.Find("Canvas").GetComponent<UIControllerScript>();
  28.         startPosition = transformPosition.position;
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     void Update()
  33.     {
  34.         //Move the object
  35.         rb2D.velocity = -transform.up * moveSpeed;
  36.  
  37.         if(Input.GetKey(KeyCode.Z) && !isPulled)
  38.         {
  39.             if(closestTower != null && hookedTower == null)
  40.             {
  41.                 hookedTower = closestTower;
  42.             }
  43.  
  44.             if(hookedTower)
  45.             {
  46.                 float distance = Vector2.Distance(transform.position, hookedTower.transform.position);
  47.  
  48.                 //gravitation toward tower
  49.                 Vector3 pullDirection = (hookedTower.transform.position - transform.position).normalized;
  50.                 float newPullForce = Mathf.Clamp(pullForce / distance, 20, 50);
  51.                 rb2D.AddForce(pullDirection * newPullForce);
  52.  
  53.                 // angular velocity
  54.                 rb2D.angularVelocity = -rotateSpeed / distance;
  55.                 isPulled = true;
  56.             }
  57.         }
  58.  
  59.         if(Input.GetKeyUp(KeyCode.Z))
  60.         {
  61.             isPulled = false;
  62.         }
  63.  
  64.         if (isCrashed)
  65.         {
  66.             restartPosition();
  67.         }
  68.         else
  69.         {
  70.             rb2D.velocity = -transform.up * moveSpeed;
  71.         }
  72.     }
  73.  
  74.     public void OnCollisionEnter2D(Collision2D collision)
  75.     {
  76.         if(collision.gameObject.tag == "Wall")
  77.         {
  78.            if(!isCrashed)
  79.            {
  80.             // play SFX
  81.             myAudio.Play();
  82.             rb2D.velocity = new Vector3(0f, 0f, 0f);
  83.             rb2D.angularVelocity = 0f;
  84.             isCrashed = true;
  85.            }
  86.         }
  87.     }
  88.  
  89.     public void OnTriggerEnter2D(Collider2D collision)
  90.     {
  91.         if(collision.gameObject.tag == "Goal")
  92.         {
  93.             Debug.Log("Levelclear!");
  94.             uiControl.endGame();
  95.         }
  96.  
  97.     }
  98.     public void OnTriggerStay2D(Collider2D collision)
  99.     {
  100.         if(collision.gameObject.tag == "Tower")
  101.         {
  102.             closestTower = collision.gameObject;
  103.  
  104.             // tukar warna tower kembali ke warna hijau
  105.             collision.gameObject.GetComponent<SpriteRenderer>().color = Color.green;
  106.         }
  107.     }
  108.     public void OnTriggerExit2D(Collider2D collision)
  109.     {
  110.         if (isPulled) return;
  111.  
  112.         if(collision.gameObject.tag == "Tower")
  113.         {
  114.             closestTower = null;
  115.  
  116.             // change tower color back to normal
  117.             collision.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
  118.         }
  119.     }
  120.  
  121.     public void restartPosition()
  122.     {
  123.         // set to start position
  124.         this.transform.position = startPosition;
  125.  
  126.         // restart rotation
  127.         this.transform.rotation = Quaternion.Euler(0f, 0f, 90f);
  128.  
  129.         //set isCrashed to false
  130.         isCrashed =false;
  131.  
  132.         if(closestTower)
  133.         {
  134.             closestTower.GetComponent<SpriteRenderer>().color = Color.white;
  135.             closestTower = null;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement