lylythechosenone

Player.cs

Feb 17th, 2021 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8.     public Transform Green;
  9.     public Transform Blue;
  10.     public float Speed;
  11.     public LayerMask EverythingLayerMask;
  12.  
  13.     private int current = 0;
  14.     private float cooldownRemaining;
  15.  
  16.     void Start()
  17.     {
  18.         cooldownRemaining = 1 / Speed;
  19.         Vector3 position = transform.position;
  20.         position = GetCharacter().position;
  21.         position = new Vector3(position.x, position.y, -5.0f);
  22.         transform.position = position;
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         Vector3 toMove = Vector3.zero;
  28.  
  29.         // Decrease cooldown
  30.         if (cooldownRemaining > 0.0f)
  31.         {
  32.             cooldownRemaining -= Time.deltaTime;
  33.         }
  34.        
  35.         // Apply controls
  36.         toMove += new Vector3(Mathf.Round(Input.GetAxisRaw("Horizontal")) * (cooldownRemaining <= 0.0f ? 1.1f : 0.0f), 0.0f, 0.0f);
  37.         toMove += new Vector3(0.0f, Mathf.Round(Input.GetAxisRaw("Vertical")) * (cooldownRemaining <= 0.0f ? 1.1f : 0.0f), 0.0f);
  38.  
  39.         // Reset cooldown
  40.         if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && cooldownRemaining <= 0.0f)
  41.         {
  42.             cooldownRemaining = 1 / Speed;
  43.         }
  44.        
  45.         // Collision
  46.         /*Collider2D[] hits;
  47.         LayerMask layerMask = ~0;
  48.         ContactFilter2D contactFilter = new ContactFilter2D();
  49.         contactFilter.SetLayerMask(layerMask);
  50.         contactFilter.useDepth = true;
  51.         contactFilter.minDepth = -Mathf.Infinity;
  52.         contactFilter.maxDepth = Mathf.Infinity;*/
  53.         Collider2D[] hits = Physics2D.OverlapCircleAll(GetCharacter().position, 1.0f, EverythingLayerMask);
  54.         if (hits.Length > 0)
  55.         {
  56.             int direction = Mathf.Abs(GetDirection(hits[0].transform.position));
  57.             if (direction == 1)
  58.             {
  59.                 toMove.x = 0;
  60.             } else if (direction == 2)
  61.             {
  62.                 toMove.y = 0;
  63.             }
  64.  
  65.             if (direction != 0)
  66.             {
  67.                 Debug.Log(direction);
  68.             }
  69.         }
  70.  
  71.         // Move
  72.         GetCharacter().position += toMove;
  73.        
  74.         // Character switching
  75.         if (Input.GetKeyDown(KeyCode.Tab))
  76.         {
  77.             current = current == 0 ? 1 : 0;
  78.         }
  79.        
  80.         // Camera
  81.         Vector3 position = transform.position;
  82.         position = Vector3.MoveTowards(position, GetCharacter().position, 0.5f);
  83.         position = new Vector3(position.x, position.y, -5);
  84.         transform.position = position;
  85.     }
  86.  
  87.     Transform GetCharacter()
  88.     {
  89.         return current == 0 ? Green : Blue;
  90.     }
  91.  
  92.     int GetDirection(Vector3 pos)
  93.     {
  94.         if (pos.x < GetCharacter().position.x)
  95.         {
  96.             return -1;
  97.         } if (pos.x > GetCharacter().position.x)
  98.         {
  99.             return 1;
  100.         } if (pos.y < GetCharacter().position.y)
  101.         {
  102.             return -2;
  103.         } if (pos.y < GetCharacter().position.y)
  104.         {
  105.             return 2;
  106.         }
  107.  
  108.         return 0;
  109.     }
  110. }
Add Comment
Please, Sign In to add comment