Help needed snake game tail problems

Hi
I’m trying to make a snake game. What I want to do is to spawn a new segment of the snake when it eats a instantiated prefab that I call dot and then make that segment a child of the first segment. The segments also have to spawn at a longer distance from the first segment the longer the snake gets.
This is the code that I have wrote.

#pragma strict

function Start () {

}
var tailend : Vector3;     
var zaxis : int;
var tailsegment : GameObject;


function OnCollisionEnter(collision : Collision){
if(collision.gameObject.name == "dot(Clone)"){

tailend = Vector3(0,0,zaxis-1);

var Instance : GameObject = Instantiate(tailsegment, tailend, transform.rotation);
Instance.transform.parent = transform;


}}

When the snake eats the dot nothing happens. I would be very thankful if someone could read it and tell me what i’m doing wrong.

Are you going to be texturing your snakes so they look realistic or just keep them as blocks?

Was just wondering cos we made a game called Snake Showdown for Android and iOS and getting a textured snake working was a %($@%%$* nightmare, the guy working on it ended up using procedural mesh generation. Just saying it is certainly not easy but if you keep it to blocks you'll be ok.

1 Answer

1

Okay, getting closer to an answer.
Minor point, change:

  if(collision.gameObject.tag == "dot"){

to:

  if(collision.gameObject.CompareTag("dot")){

Seems weird, but the CompareTag() function is faster.

Looks like the problem is with the tailend = Vector3(0,0,zaxis-1); line.
The Instantiate() function will use the tailend coordinates in World Space, when it appears you meant them to be relative to the position of the head of your snake.
You could try:

tailend = Vector3(transform.position.x, transform.position.y, transform.position.z - zaxis - 1);

I’m not entirely sure what’s in “zaxis”, or how you’re structuring your snake and your movement code, so that might not be entirely accurate, but hopefully it leads you in the right direction.