-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrain.cs
69 lines (58 loc) · 1.51 KB
/
Brain.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brain : MonoBehaviour
{
int DNALength = 2;
public DNA dna;
public GameObject eyes;
bool seeWall = true;
Vector3 startPosition;
public float distanceTravelled = 0;
bool alive = true;
public void Init()
{
// init DNA
// 0 forward
// 1 Angle turn
dna = new DNA(DNALength, 360);
startPosition = this.transform.position;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "dead")
{
distanceTravelled = 0;
alive = false;
}
}
// Update is called once per frame
void Update()
{
if (!alive) return;
seeWall = false;
RaycastHit hit;
Debug.DrawRay(eyes.transform.position, eyes.transform.forward * 0.5f, Color.magenta);
if(Physics.SphereCast(eyes.transform.position,0.1f,eyes.transform.forward,out hit, 0.5f))
{
if (hit.collider.gameObject.tag == "wall")
{
seeWall = true;
}
}
}
void FixedUpdate()
{
if (!alive) return;
//read dna
float h = 0;
float v = dna.GetGene(0);
if (seeWall)
{
h = dna.GetGene(1);
}
this.transform.Translate(0, 0, v * 0.0004f);
this.transform.Rotate(0, h, 0);
distanceTravelled = Vector3.Distance(startPosition, this.transform.position);
}
}