Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- public Transform Green;
- public Transform Blue;
- public float Speed;
- public LayerMask EverythingLayerMask;
- private int current = 0;
- private float cooldownRemaining;
- void Start()
- {
- cooldownRemaining = 1 / Speed;
- Vector3 position = transform.position;
- position = GetCharacter().position;
- position = new Vector3(position.x, position.y, -5.0f);
- transform.position = position;
- }
- void Update()
- {
- Vector3 toMove = Vector3.zero;
- // Decrease cooldown
- if (cooldownRemaining > 0.0f)
- {
- cooldownRemaining -= Time.deltaTime;
- }
- // Apply controls
- toMove += new Vector3(Mathf.Round(Input.GetAxisRaw("Horizontal")) * (cooldownRemaining <= 0.0f ? 1.1f : 0.0f), 0.0f, 0.0f);
- toMove += new Vector3(0.0f, Mathf.Round(Input.GetAxisRaw("Vertical")) * (cooldownRemaining <= 0.0f ? 1.1f : 0.0f), 0.0f);
- // Reset cooldown
- if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && cooldownRemaining <= 0.0f)
- {
- cooldownRemaining = 1 / Speed;
- }
- // Collision
- /*Collider2D[] hits;
- LayerMask layerMask = ~0;
- ContactFilter2D contactFilter = new ContactFilter2D();
- contactFilter.SetLayerMask(layerMask);
- contactFilter.useDepth = true;
- contactFilter.minDepth = -Mathf.Infinity;
- contactFilter.maxDepth = Mathf.Infinity;*/
- Collider2D[] hits = Physics2D.OverlapCircleAll(GetCharacter().position, 1.0f, EverythingLayerMask);
- if (hits.Length > 0)
- {
- int direction = Mathf.Abs(GetDirection(hits[0].transform.position));
- if (direction == 1)
- {
- toMove.x = 0;
- } else if (direction == 2)
- {
- toMove.y = 0;
- }
- if (direction != 0)
- {
- Debug.Log(direction);
- }
- }
- // Move
- GetCharacter().position += toMove;
- // Character switching
- if (Input.GetKeyDown(KeyCode.Tab))
- {
- current = current == 0 ? 1 : 0;
- }
- // Camera
- Vector3 position = transform.position;
- position = Vector3.MoveTowards(position, GetCharacter().position, 0.5f);
- position = new Vector3(position.x, position.y, -5);
- transform.position = position;
- }
- Transform GetCharacter()
- {
- return current == 0 ? Green : Blue;
- }
- int GetDirection(Vector3 pos)
- {
- if (pos.x < GetCharacter().position.x)
- {
- return -1;
- } if (pos.x > GetCharacter().position.x)
- {
- return 1;
- } if (pos.y < GetCharacter().position.y)
- {
- return -2;
- } if (pos.y < GetCharacter().position.y)
- {
- return 2;
- }
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment