Advertisement
Guest User

Snake.cs

a guest
Mar 2nd, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Snake : MonoBehaviour {
  6.  
  7.     private Vector2Int gridMoveDirection;
  8.     private Vector2Int lastMoveDirection;
  9.     private Vector2Int gridPosition;
  10.     private float gridMoveTimer;
  11.     private float gridMoveTimeMax;
  12.    
  13.  
  14.     private void Awake() {
  15.         gridPosition = new Vector2Int(10, 10);
  16.         gridMoveTimeMax = .65f;
  17.         gridMoveTimer = gridMoveTimeMax;
  18.         gridMoveDirection = new Vector2Int(1, 0);
  19.         lastMoveDirection = new Vector2Int(1, 0);
  20.     }
  21.  
  22.     private void Update() {
  23.         HandleInput();
  24.         HandlerGridMovement();
  25.  
  26.     }
  27.  
  28.     private void HandleInput() {
  29.         if (Input.GetKeyDown(KeyCode.UpArrow))
  30.         {
  31.             if (lastMoveDirection.y != -1)
  32.             {
  33.                 gridMoveDirection.x = 0;
  34.                 gridMoveDirection.y = 1;
  35.             }
  36.  
  37.         }
  38.         if (Input.GetKeyDown(KeyCode.DownArrow))
  39.         {
  40.             if (lastMoveDirection.y != 1)
  41.             {
  42.                 gridMoveDirection.x = 0;
  43.                 gridMoveDirection.y = -1;
  44.             }
  45.         }
  46.         if (Input.GetKeyDown(KeyCode.LeftArrow))
  47.         {
  48.             if (lastMoveDirection.x != 1)
  49.             {
  50.                 gridMoveDirection.x = -1;
  51.                 gridMoveDirection.y = 0;
  52.             }
  53.         }
  54.         if (Input.GetKeyDown(KeyCode.RightArrow))
  55.         {
  56.             if (lastMoveDirection.x != -1)
  57.             {
  58.                 gridMoveDirection.x = 1;
  59.                 gridMoveDirection.y = 0;
  60.             }
  61.         }
  62.  
  63.  
  64.     }
  65.     private void HandlerGridMovement() {
  66.         gridMoveTimer += Time.deltaTime;
  67.         if (gridMoveTimer >= gridMoveTimeMax)
  68.         {
  69.             gridPosition += gridMoveDirection;
  70.             gridMoveTimer -= gridMoveTimeMax;
  71.             //gridPosition += gridMoveDirection;
  72.  
  73.             transform.position = new Vector3(gridPosition.x, gridPosition.y);
  74.             transform.eulerAngles = new Vector3(0, 0, GetAngleFromVector(gridMoveDirection) - 90);
  75.             lastMoveDirection = new Vector2Int(gridMoveDirection.x, gridMoveDirection.y);
  76.         }
  77.     }
  78.  
  79.     private float GetAngleFromVector(Vector2Int dir) {
  80.         float n = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  81.         if (n < 0) n += 360;
  82.         return n;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement