Help part of snake dispearing on play

I am following this tutorial to make a snake game.

I haven’t been able to get the assets the tutorial uses so have made my own in unity. I’ve used a cube stretched for walls plane for the ground and spheres for the snake. I have got just past half hour into it where you do player controller script. I’ve copied the script it tells you like this.

public class PlayerController : MonoBehaviour
{
    [HideInInspector]
    public PlayerDirection direction;
    [HideInInspector]
    public float step_Length = 0.2f;
    [HideInInspector]
    public float movement_Frequency = 0.1f;

    private float counter;
    private bool move;

    [SerializeField]
    private GameObject tailPrefab;

    private List<Vector3> delta_Position;

    public List<Rigidbody> nodes;

    private Rigidbody main_Body;
    private Rigidbody head_Body;
    private Transform tr;

    private bool create_Node_At_Tail;

    // Start is called before the first frame update
    void Awake()
    {
        tr = transform;
        main_Body = GetComponent<Rigidbody>();

        InitSnakeNodes();
        InitPlayer();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void InitSnakeNodes()
    {
        nodes = new List<Rigidbody>();

        nodes.Add(tr.GetChild(0).GetComponent<Rigidbody>());
        nodes.Add(tr.GetChild(1).GetComponent<Rigidbody>());
        nodes.Add(tr.GetChild(2).GetComponent<Rigidbody>());

        head_Body = nodes[0];
    }

    void SetDirectionRandom()
    {
        int dirRandom = Random.Range(0, (int)PlayerDirection.COUNT);
        direction = (PlayerDirection)dirRandom;
    }

    void InitPlayer()
    {
        SetDirectionRandom();

        switch (direction)
        {
            case PlayerDirection.RIGHT:

                nodes[1].position = nodes[0].position - new Vector3(Metrics.NODE, 0f, 0f);
                nodes[2].position = nodes[0].position - new Vector3(Metrics.NODE * 8f, 0f, 0f);

                break;

            case PlayerDirection.LEFT:

                nodes[1].position = nodes[0].position + new Vector3(Metrics.NODE, 0f, 0f);
                nodes[2].position = nodes[0].position + new Vector3(Metrics.NODE * 8f, 0f, 0f);

                break;

            case PlayerDirection.UP:

                nodes[1].position = nodes[0].position - new Vector3(0, Metrics.NODE, 0f);
                nodes[2].position = nodes[0].position - new Vector3(0, Metrics.NODE * 8f, 0f);

                break;

            case PlayerDirection.DOWN:

                nodes[1].position = nodes[0].position + new Vector3(0, Metrics.NODE, 0f);
                nodes[2].position = nodes[0].position + new Vector3(0, Metrics.NODE * 8f, 0f);

                break;
        }
    }
}

But when I press play the centre sphere disapears. I have done the obvious and unchecked gravity in the inspector. Any ideas why its disapearing when I press play? The head 1st and tail 3rd sphere stay as they should just middle one disapears.

Any ideas what’s wrong?

You don’t need to bump your threads in order to get assistance. Unanswered posts are actually more likely to get an answer from me, since once I start seeing activity on the replies count, I tend to assume that person is already getting helped.

Since you simply copied code from somewhere else, I won’t bother addressing that, since it’s unlikely that’s your problem. That means it’s probably some kind of Editor setting. You’ll have to post screenshots or a video or something of what’s happening so we can get a better idea, though. There’s not much we can tell from the description you’ve given.

Screenshots of what exactly?

Everything? Anything? What I’d really like to see is a video that actually shows the spheres disappearing from view, particularly with the Scene view camera positioned so we can see if they’re falling through the ground, and the Inspector visible with one of the spheres selected so I can see its Transform values.

1 Like

Can you tell me how I can get and post a video on here? or if you have kik you could add me on there sarahb27986 and I can send you the video on there.

Ive just tested it the bit that seems to disappear does seem to still be there but inside the head sphere. I’ve left it selected in hierarchy so I can see the outline of it. If you explain how I can post a video of it on here or add me on kik I will show you a video.

There’s an “Upload a File” button right next to “Post Reply.” Does that not work?

Edit: I’m practically a grandpa at the ripe old age of 37, so I don’t know what a Kik is. :stuck_out_tongue:

It won’t let me add videos but this is basically what happens the 3 spheres is before pressing play then 2 spheres after and the node is selected in hierarchy.


It doesn’t look like it disappeared. It looks like it is mostly inside the ball to the right.

1 Like

This is why I always recommend against copying any code you find in tutorials. You’ve now got a player controller that you don’t understand how it works when it’s functioning correctly, much less when something is going wrong.

Out of curiosity, have you continued along in the tutorial to see if the person you’re copying from mentions this issue and fixes it later?

2 Likes

The person shows the snake appearing as it should with 3 spheres. I copied more code it said to get the snake to move and got errors in the script. So now I’m taking a different approach. I’ve found a long single script for a snake game that you just add to an empty game object and it works. So I’m using that to try to build a snake game myself that I understand. I know bits of some code so going to test things out see what happens. As a few say on here tutorrials aren’t always the best way as you rely on them then have no idea how to solve problems that come up.

There’s nothing wrong with following along with tutorials. What’s important is that you understand what you’re doing and why you’re doing it. It might not be immediately clear from the video/article you’re following, so it’s on you to read into things, play around and break stuff, and figure it out, too.

You’re, of course, perfectly welcome to do things how you like. But if you don’t learn how to solve the problems you run into, your entire journey as a game developer will just be a loop of frustrated jumping from task to task.

Good luck, though!

1 Like

I’ve reviewed your code and it sure looks like what is in the video too, so I’m not sure what the issue might be. I see you asked the author too on YouTube. I try to find tutorials that also provide the source code (but only use it if you get stuck!)

1 Like

There’s a similar game on Xbox already called Snake Pass, I think it was made in Unity.