leomovskii

PlayerScript.cs

Oct 28th, 2021
772
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerScript : MonoBehaviour {
  6.  
  7.     Rigidbody2D rb;
  8.  
  9.     public float moveSpeed = 3f;
  10.     public float jumpPower = 250f;
  11.  
  12.     Vector3 respawnLocation;
  13.     public Vector3 teleportLocation = new Vector3(-9.5f, 3f, 0f);
  14.  
  15.     public int lives = 0;
  16.     public int gold = 0;
  17.  
  18.     public Transform teleportTarget;
  19.  
  20.     public GameObject[] fakePlatforms;
  21.  
  22.     bool isGrounded;
  23.  
  24.     void Start() {
  25.         rb = GetComponent<Rigidbody2D>();
  26.     }
  27.  
  28.     void Update() {
  29.         float moveX = Input.GetAxis("Horizontal");
  30.         float moveY = Input.GetAxis("Vertical");
  31.        
  32.         if (moveX > 0) {
  33.             transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, transform.eulerAngles.z);
  34.         } else if (moveX < 0) {
  35.             transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z);
  36.         }
  37.        
  38.         rb.velocity = new Vector2(moveX * moveSpeed, rb.velocity.y);
  39.        
  40.         if (isGrounded && moveY > 0) {
  41.             rb.AddForce(Vector2.up * jumpPower);
  42.             isGrounded = false;
  43.         }
  44.     }
  45.    
  46.     void OnTriggerEnter2D(Collider2D col) {
  47.         if (col.gameObject.tag == "Saw" || col.gameObject.tag == "Fall") {
  48.             lives++;
  49.             Debug.Log("Lives: " + lives.ToString());
  50.             transform.position = respawnLocation;
  51.  
  52.         } else if (col.gameObject.tag == "Gold") {
  53.             Destroy(col.gameObject);
  54.             gold++;
  55.             Debug.Log("Gold: " + gold.ToString());
  56.            
  57.         } else if (col.gameObject.tag == "Next") {
  58.             transform.position = teleportLocation;
  59.            
  60.         } else if (col.gameObject.tag == "Exit") {
  61.             Debug.Log("You Win! Lives: " + lives.ToString());
  62.             Destroy(gameObject);
  63.            
  64.         } else if (col.gameObject.tag == "Respawn") {
  65.             respawnLocation = col.gameObject.transform.position;
  66.             Destroy(col.gameObject);
  67.         }
  68.     }
  69.    
  70.     void OnCollisionEnter2D(Collision2D col) {
  71.         if (col.gameObject.tag == "Ground") {
  72.             isGrounded = true;
  73.         } else if (col.gameObject.tag == "Platform") {
  74.             isGrounded = true;
  75.             transform.parent = col.gameObject.transform;
  76.         } else if (col.gameObject.tag == "Button") {
  77.             col.gameObject.transform.position = new Vector3(col.gameObject.transform.position.x, col.gameObject.transform.position.y - 0.2f, col.gameObject.transform.position.z);
  78.            
  79.             foreach (GameObject go in fakePlatforms)
  80.                 go.GetComponent<BoxCollider2D>().enabled = true;
  81.         } else if (col.gameObject.tag == "Gravity") {
  82.             col.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1f;
  83.         }
  84.     }
  85.    
  86.     void OnCollisionExit2D(Collision2D col) {
  87.         if (col.gameObject.tag == "Platform") {
  88.             transform.parent = null;
  89.         }
  90.     }
  91. }
RAW Paste Data