-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeController.cs
70 lines (59 loc) · 1.71 KB
/
SnakeController.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
70
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class SnakeController : MonoBehaviour {
public List<Transform> Tails;
[Range(0,3)] public float BonesDistance;
public GameObject BonePrefab;
[Range(0, 4)] public float Speed;
private Transform _transform;
public int k = 1;
public Text ScoreText;
public int Score = 0;
public UnityEvent OnEat;
private void Start()
{
_transform = GetComponent<Transform>();
}
private void Update()
{
ScoreText.text = "Score: "+Score.ToString();
MoveSnake(_transform.position+_transform.forward*Speed*k);
float angel = Input.GetAxis("Horizontal")*4;
_transform.Rotate(0, angel, 0);
}
private void MoveSnake(Vector3 newPosition)
{
float sqrDistanse = BonesDistance * BonesDistance;
_transform.position = newPosition;
Vector3 previosPosition = _transform.position;
foreach (var bone in Tails)
{
if((bone.position - previosPosition).sqrMagnitude>sqrDistanse)
{
var temp = bone.position;
bone.position = previosPosition;
previosPosition = temp;
} else
{
break;
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Food")
{
Score++;
Destroy(collision.gameObject);
var bone = Instantiate(BonePrefab);
Tails.Add(bone.transform);
if (OnEat != null)
{
OnEat.Invoke();
}
}
}
}