Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sasherafat-zz/10ffe5bdbdecc06f29513e80f970e881 to your computer and use it in GitHub Desktop.
Save sasherafat-zz/10ffe5bdbdecc06f29513e80f970e881 to your computer and use it in GitHub Desktop.
full snake game script
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