Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerScript : MonoBehaviour {
- Rigidbody2D rb;
- public float moveSpeed = 3f;
- public float jumpPower = 250f;
- Vector3 respawnLocation;
- public Vector3 teleportLocation = new Vector3(-9.5f, 3f, 0f);
- public int lives = 0;
- public int gold = 0;
- public Transform teleportTarget;
- public GameObject[] fakePlatforms;
- bool isGrounded;
- void Start() {
- rb = GetComponent<Rigidbody2D>();
- }
- void Update() {
- float moveX = Input.GetAxis("Horizontal");
- float moveY = Input.GetAxis("Vertical");
- if (moveX > 0) {
- transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, transform.eulerAngles.z);
- } else if (moveX < 0) {
- transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z);
- }
- rb.velocity = new Vector2(moveX * moveSpeed, rb.velocity.y);
- if (isGrounded && moveY > 0) {
- rb.AddForce(Vector2.up * jumpPower);
- isGrounded = false;
- }
- }
- void OnTriggerEnter2D(Collider2D col) {
- if (col.gameObject.tag == "Saw" || col.gameObject.tag == "Fall") {
- lives++;
- Debug.Log("Lives: " + lives.ToString());
- transform.position = respawnLocation;
- } else if (col.gameObject.tag == "Gold") {
- Destroy(col.gameObject);
- gold++;
- Debug.Log("Gold: " + gold.ToString());
- } else if (col.gameObject.tag == "Next") {
- transform.position = teleportLocation;
- } else if (col.gameObject.tag == "Exit") {
- Debug.Log("You Win! Lives: " + lives.ToString());
- Destroy(gameObject);
- } else if (col.gameObject.tag == "Respawn") {
- respawnLocation = col.gameObject.transform.position;
- Destroy(col.gameObject);
- }
- }
- void OnCollisionEnter2D(Collision2D col) {
- if (col.gameObject.tag == "Ground") {
- isGrounded = true;
- } else if (col.gameObject.tag == "Platform") {
- isGrounded = true;
- transform.parent = col.gameObject.transform;
- } else if (col.gameObject.tag == "Button") {
- col.gameObject.transform.position = new Vector3(col.gameObject.transform.position.x, col.gameObject.transform.position.y - 0.2f, col.gameObject.transform.position.z);
- foreach (GameObject go in fakePlatforms)
- go.GetComponent<BoxCollider2D>().enabled = true;
- } else if (col.gameObject.tag == "Gravity") {
- col.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1f;
- }
- }
- void OnCollisionExit2D(Collision2D col) {
- if (col.gameObject.tag == "Platform") {
- transform.parent = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement