Advertisement
powerofsoul

Untitled

Mar 10th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. namespace Game {
  8.     public class Player : MonoBehaviour {
  9.  
  10.         private Vector3 PlayerStartPosition;
  11.         public Text score;
  12.  
  13.         public float SpeedMultiplayer = 1;
  14.        
  15.         private Camera _mainCamera {
  16.             get {
  17.                 return Camera.main;
  18.             }
  19.         }
  20.         //TO DO remove me later
  21.         public bool playing = false;
  22.  
  23.         void Start() {
  24.             PlayerStartPosition = gameObject.transform.position;
  25.         }
  26.  
  27.         public void Update() {
  28.             if (Input.GetMouseButtonDown(0))
  29.                 playing = true;
  30.             if (playing) {
  31.                 CheckCollider();
  32.                 MoveForward();
  33.                 MoveSideWays();
  34.             } else {
  35.                 gameObject.transform.position = PlayerStartPosition;
  36.             }
  37.         }
  38.  
  39.         private bool Clicked = false;
  40.         private float displacement = 0;
  41.         private void MoveSideWays() {
  42.             if (!Clicked && Input.GetMouseButton(0)) {
  43.                 displacement = WorldPoint(Input.mousePosition).x - gameObject.transform.position.x;
  44.                 Clicked = true;
  45.             } else if (Clicked && !Input.GetMouseButton(0)) Clicked = false;
  46.             else {
  47.                 gameObject.transform.position = new Vector3(
  48.                         WorldPoint(Input.mousePosition).x - displacement,
  49.                         gameObject.transform.position.y,
  50.                         gameObject.transform.position.z
  51.                     );
  52.             }
  53.  
  54.  
  55.         }
  56.  
  57.         private Vector3 WorldPoint(Vector3 screenpoint) {
  58.             return _mainCamera.ScreenToWorldPoint(screenpoint);
  59.         }
  60.  
  61.         private void MoveForward() {
  62.             var v2 = gameObject.transform.position.ToVector2();
  63.             Vector2 positionTo = Vector2.Lerp(v2, v2 + Vector2.up, 0.01f * SpeedMultiplayer);
  64.             gameObject.transform.position = positionTo.ToVector3(gameObject.transform.position.z);
  65.         }
  66.  
  67.         private bool CheckCollider() {
  68.             var hit = Physics2D.CircleCast(gameObject.transform.position.ToVector2(), 0.2f, Vector2.zero);
  69.             if (hit.collider == null)
  70.                 return false;
  71.             switch (hit.collider.name) {
  72.                 case "hole":
  73.                     Debug.Log(hit.collider.name);
  74.                     this.transform.position = PlayerStartPosition;
  75.                     _mainCamera.GetComponent<Game>().GenerateNextMap();
  76.                     score.text = (int.Parse(score.text) + 1).ToString();
  77.                     playing = false;
  78.                     break;
  79.                 default:
  80.                     Debug.Log(hit.collider.name);
  81.                     this.transform.position = PlayerStartPosition;
  82.                     _mainCamera.GetComponent<Game>().ResetMap();
  83.                     score.text = 0.ToString();
  84.                     playing = false;
  85.                     break;
  86.             }
  87.             return true;
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement