- Home /
making snake game but the code is confusing
Hi, so I've started to learn how to make snake game in unity today from a toutorial but the code was kind of bewildering especially for me beacuase I'm a begginer at unity and I'm still eleven years old. So here is the code in the toutorial that perplexed me
void Move() {
// Save current position (gap will be here)
Vector2 v = transform.position;
// Move head into new direction (now there is a gap)
transform.Translate(dir);
// Do we have a Tail?
if (tail.Count > 0) {
tail.Last().position = v;
}
so basically what we are doing is we have created the head of the snake and we have also made a list of tales for the snake's body and the idea is that when the head for the snake moves in a certain direction the last element in the tales list will move to the gap that the head had made; so where the snake's head was before, but in the toutorial they didn't say that the last element in the tales list will move to the gap that the snake's head had made, they said that the last element in the tale's list will move to the position(transform.position) of the snake's head not to the gap that the snake's head had made. so that was confusing beacuse when I ran it what happend was that when the head moved in a certain direction the last element in the snake's body moved to the gap that the head's snake had made not to the position of the snake's head because in the code they said that they will move the last element of the tale's list to the transform.position of the snake's head. Do you know why I'm wrong? and do you know why that happens and how it works. thank you.
And I forgot to say that I called the Move fuction in the start method like this InvokeRepeating("Move", 0.3f,0.3f);
Answer by unity_ek98vnTRplGj8Q · Jun 01, 2021 at 03:36 PM
When captures the position of the head in this line:
Vector2 v = transform.position;
it is looking at where the snake's head position is before it has moved. When the head does finally move in this line
transform.Translate(dir);
The head is at a different position but v
still has the value of the old head position. So when we move the tail to position v
it goes to where the head was before it moved.
Now you may ask yourself why v
refers to the old position rather then the new position. Look at the following code:
int i = 0;
int j = i;
i = i + 1;
//At this point, j = 0 and i = 1
When we call j = i
, j is getting a copy of the value of i, but does not refer to the value of i directly. So if i changes later, j does NOT change. The same thing happens with Vectors or any other struct in C#.
Vector2 i = new Vector2(0, 0);
Vector2 j = i;
i = i + new Vector2(1, 1);
//At this point j = (0, 0) and i = (1, 1)
So hopefully that answers your question as to why the tail moves to the gap where the head was rather than on top of the head.
It is important to note that this is different for any Class types in C#. Consider the following code
class MyClass {
public int value;
}
MyClass a = new MyClass();
a.value = 0;
MyClass b = a;
a.value = a.value + 1;
//At this point a.value = 1 AND b.value = 1
This is one of the very important differences between Classes and Structs in C#. Structs are pass by value and classes are pass by reference. Vector3's are structs, and are therefore passed by value, meaning that when you pass one struct to another the seconds struct gets a copy of the original data, not the original data itself.
Ok thank you very very very much now I understand it too well
I thought structs are reference types that's why I got confused
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
My script only partially works 2 Answers
Disable teleporter after being used 1 Answer
Get Random Number for each player from Dice 1 Answer
Multiple Cars not working 1 Answer