Search Unity

Showcase How to make a snake in Unity C#

Discussion in 'Scripting' started by Noodle_Meanie, Mar 21, 2023.

  1. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110
    Here is how I made huge snake for my game. Sneak peek of the final result: upload_2023-3-20_18-8-43.png

    The first thing I did for this was make a small prototype in Scratch to see if my method would work, of course this prototype doesn't work well, but it gives you an idea of what it could look like: Snake test on Scratch (mit.edu).

    After the prototype, I set it up in Unity. Here is the code (note that any values or names can be changed if wanted):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SnakePart : MonoBehaviour
    6. {
    7.     public GameObject nextPart; //This is what you want to follow.
    8.     public float distance; //This float gets how far away the next part is.
    9.     void Update()
    10.     {
    11.  
    12.         //This is how I make a snake in unity c#. This system can be used for many other things aswell, and allow infinite parts to be added. Created by Noodle Meanie Games.
    13.  
    14.         distance = Vector2.Distance(transform.position, nextPart.transform.position); //This sets our float to the distance between each gameObject
    15.  
    16.         if (1.4 <= distance)
    17.         {
    18.             //Debug.Log("the distance is" + distance);  // You can use this to test the distance.
    19.  
    20.             if (tag == "SnakeBossFirst") // This tag is put onto the first part of the body, not the head, the first part of the body NEXT to the head.
    21.             {
    22.                 transform.position = Vector2.MoveTowards(transform.position, nextPart.transform.position, distance * 15 * Time.deltaTime); //Moves the first part to the head.
    23.                 Debug.Log("egg"); //Testing to make sure it works.
    24.             }
    25.             else // for parts that aren't the first.
    26.             {
    27.                 transform.position = Vector2.MoveTowards(transform.position, nextPart.transform.position, distance * 7 * Time.deltaTime); //Moves any other parts. Note: any of these values can be changed to suit your snake.
    28.             }
    29.         }
    30.         else //If the parts are closer together:
    31.         {
    32.             if (1.1 <= distance) //Checks how close.
    33.             {
    34.                 //Debug.Log("the distance is" + distance); //Testing.
    35.  
    36.                 if (tag == "SnakeBossFirst") //The rest is the same, besides how fast the parts move.
    37.                 {
    38.                     transform.position = Vector2.MoveTowards(transform.position, nextPart.transform.position, distance * 7 * Time.deltaTime);
    39.                     Debug.Log("egg");
    40.                 }
    41.                 else
    42.                 {
    43.                     transform.position = Vector2.MoveTowards(transform.position, nextPart.transform.position, distance * 3 * Time.deltaTime);
    44.                 } //The purpose of this is to smooth it out when the body parts are closer together. This isn't really required if your snake casually moves slow.
    45.             }
    46.         }
    47.  
    48.         //Snake code by Noodle Meanie Games.
    49.        
    50.     }
    51. }
    Here you can see how the first body part needs a special tag: upload_2023-3-20_17-50-52.png

    Here you can see that each part is assigned to the part it wants to follow: upload_2023-3-20_17-52-5.png

    Sadly, I couldn't get a video to show the final result, but I did get a photo: upload_2023-3-20_18-8-48.png
    Remember that as many parts as you want can be added.

    If you have any questions about the script, or how to set it up, please ask in the comments below. Remember that you may have to change some of the values depending on the size, wanted speed, and length of gaps, of your snake. This does not have to be for a snake.

    Hope this helps someone out there.
     

    Attached Files:

    Olipool likes this.