Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Game {
- public class Player : MonoBehaviour {
- private Vector3 PlayerStartPosition;
- public Text score;
- public float SpeedMultiplayer = 1;
- private Camera _mainCamera {
- get {
- return Camera.main;
- }
- }
- //TO DO remove me later
- public bool playing = false;
- void Start() {
- PlayerStartPosition = gameObject.transform.position;
- }
- public void Update() {
- if (Input.GetMouseButtonDown(0))
- playing = true;
- if (playing) {
- CheckCollider();
- MoveForward();
- MoveSideWays();
- } else {
- gameObject.transform.position = PlayerStartPosition;
- }
- }
- private bool Clicked = false;
- private float displacement = 0;
- private void MoveSideWays() {
- if (!Clicked && Input.GetMouseButton(0)) {
- displacement = WorldPoint(Input.mousePosition).x - gameObject.transform.position.x;
- Clicked = true;
- } else if (Clicked && !Input.GetMouseButton(0)) Clicked = false;
- else {
- gameObject.transform.position = new Vector3(
- WorldPoint(Input.mousePosition).x - displacement,
- gameObject.transform.position.y,
- gameObject.transform.position.z
- );
- }
- }
- private Vector3 WorldPoint(Vector3 screenpoint) {
- return _mainCamera.ScreenToWorldPoint(screenpoint);
- }
- private void MoveForward() {
- var v2 = gameObject.transform.position.ToVector2();
- Vector2 positionTo = Vector2.Lerp(v2, v2 + Vector2.up, 0.01f * SpeedMultiplayer);
- gameObject.transform.position = positionTo.ToVector3(gameObject.transform.position.z);
- }
- private bool CheckCollider() {
- var hit = Physics2D.CircleCast(gameObject.transform.position.ToVector2(), 0.2f, Vector2.zero);
- if (hit.collider == null)
- return false;
- switch (hit.collider.name) {
- case "hole":
- Debug.Log(hit.collider.name);
- this.transform.position = PlayerStartPosition;
- _mainCamera.GetComponent<Game>().GenerateNextMap();
- score.text = (int.Parse(score.text) + 1).ToString();
- playing = false;
- break;
- default:
- Debug.Log(hit.collider.name);
- this.transform.position = PlayerStartPosition;
- _mainCamera.GetComponent<Game>().ResetMap();
- score.text = 0.ToString();
- playing = false;
- break;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement