Created
August 22, 2019 08:11
-
-
Save sasherafat-zz/10ffe5bdbdecc06f29513e80f970e881 to your computer and use it in GitHub Desktop.
full snake game script
This file contains hidden or 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 UnityEngine; | |
using System.Collections; | |
public class SpawnFood : MonoBehaviour { | |
// Food Prefab | |
public GameObject foodPrefab; | |
// Borders | |
public Transform borderTop; | |
public Transform borderBottom; | |
public Transform borderLeft; | |
public Transform borderRight; | |
// Use this for initialization | |
void Start () { | |
// Spawn food every 4 seconds, starting in 3 | |
InvokeRepeating("Spawn", 3, 4); | |
} | |
// Spawn one piece of food | |
void Spawn() { | |
// x position between left & right border | |
int x = (int)Random.Range(borderLeft.position.x, | |
borderRight.position.x); | |
// y position between top & bottom border | |
int y = (int)Random.Range(borderBottom.position.y, | |
borderTop.position.y); | |
// Instantiate the food at (x, y) | |
Instantiate(foodPrefab, | |
new Vector2(x, y), | |
Quaternion.identity); // default rotation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment