-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateFood.cs
More file actions
54 lines (39 loc) · 1.39 KB
/
CreateFood.cs
File metadata and controls
54 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateFood : MonoBehaviour
{
public float timer;
public int newTarget;
// Start is called before the first frame update
void Start(){
generateFood(500);
}
// Update is called once per frame
void Update()
{
float numFood = gameObject.transform.childCount;
timer += Time.deltaTime;
if ((timer >= 10)){
generateFood(2);
print("newFood Generated");
timer = 0;
}
}
void generateFood(int quantity) {
Material food = Resources.Load("Food", typeof(Material)) as Material;
GameObject sphere;
for (int i = 1; i <= quantity; i++){
sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(Random.Range(-20,20),1,Random.Range(-20,20));
sphere.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
sphere.GetComponent<Renderer>().material = food;
sphere.AddComponent<FoodAction>();
sphere.tag = "Food";
sphere.name = "Food" + i;
sphere.transform.SetParent(gameObject.transform);
Rigidbody rb = sphere.AddComponent<Rigidbody>();
rb.mass = 1;
}
}
}