Created
November 8, 2022 20:02
-
-
Save wojciech-bilicki/7bd23de5a87a5683114358858d372c4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using Unity.VisualScripting; | |
using UnityEngine; | |
public class Snake : MonoBehaviour // Pilnować nazwy klasy aby zgadzała się z nazwą pliku - inaczej nie będzie działać | |
{ // klasa dziedziczy po MonoBehaviour | |
[SerializeField]//DEKORATOR - w unity tworzy okno ktore porzyjmie wartość | |
private float speed = 100; | |
private Vector2 direction = Vector2.zero; | |
// Start is called before the first frame update | |
private void Start() | |
{ | |
InvokeRepeating("UpdatePosition", 0, 1 / speed); //Zmieniamy prędkość kwadratu | |
} | |
private void Update() | |
{ | |
float inputX = Input.GetAxis("Horizontal");//Wbudowane funkcje odczytujace działania gracza (WSAD) | |
float inputY = Input.GetAxis("Vertical"); | |
if (inputX > 0) | |
{ | |
direction = Vector2.right; | |
} | |
else if(inputX < 0) | |
{ | |
direction = Vector2.left; | |
} | |
else if(inputY > 0) | |
{ | |
direction = Vector2.up; | |
} | |
else if(inputY < 0) | |
{ | |
direction = Vector2.down; | |
} | |
//Aby zatrzymać węża direction = Vector2.zero. Wąż ma posruszac się bez zatrzymania dlatego pomijamy ten etap. | |
private void OnTriggerEnter2D(Collider2D collision) | |
{ | |
if(collision.CompareTag("Wall"))//Sprawdzamy czy kolizja nastapiła ze ścianą | |
{ | |
Debug.Log("Wall"); | |
} | |
else if (collision.CompareTag("Food"))//Sprawdzamy czy kolizja nastapiła z jedzeniem | |
{ | |
Debug.Log("Food"); | |
} | |
} | |
private void UpdatePosition() | |
{ | |
Vector3 currentPosition = transform.position; //Odczytujemy pozycję kwadratu w unity (z osi X,Y,Z) | |
Vector3 newPosition = new Vector3( | |
currentPosition.x + direction.x, | |
currentPosition.y + direction.y, | |
0); | |
transform.position = newPosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment