Search Unity

Help part of snake dispearing on play

Discussion in 'Getting Started' started by kmo86, Apr 7, 2021.

  1. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     [HideInInspector]
    4.     public PlayerDirection direction;
    5.     [HideInInspector]
    6.     public float step_Length = 0.2f;
    7.     [HideInInspector]
    8.     public float movement_Frequency = 0.1f;
    9.  
    10.     private float counter;
    11.     private bool move;
    12.  
    13.     [SerializeField]
    14.     private GameObject tailPrefab;
    15.  
    16.     private List<Vector3> delta_Position;
    17.  
    18.     public List<Rigidbody> nodes;
    19.  
    20.     private Rigidbody main_Body;
    21.     private Rigidbody head_Body;
    22.     private Transform tr;
    23.  
    24.     private bool create_Node_At_Tail;
    25.  
    26.     // Start is called before the first frame update
    27.     void Awake()
    28.     {
    29.         tr = transform;
    30.         main_Body = GetComponent<Rigidbody>();
    31.  
    32.         InitSnakeNodes();
    33.         InitPlayer();
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.        
    40.     }
    41.  
    42.     void InitSnakeNodes()
    43.     {
    44.         nodes = new List<Rigidbody>();
    45.  
    46.         nodes.Add(tr.GetChild(0).GetComponent<Rigidbody>());
    47.         nodes.Add(tr.GetChild(1).GetComponent<Rigidbody>());
    48.         nodes.Add(tr.GetChild(2).GetComponent<Rigidbody>());
    49.  
    50.         head_Body = nodes[0];
    51.     }
    52.  
    53.     void SetDirectionRandom()
    54.     {
    55.         int dirRandom = Random.Range(0, (int)PlayerDirection.COUNT);
    56.         direction = (PlayerDirection)dirRandom;
    57.     }
    58.  
    59.     void InitPlayer()
    60.     {
    61.         SetDirectionRandom();
    62.  
    63.         switch (direction)
    64.         {
    65.             case PlayerDirection.RIGHT:
    66.  
    67.                 nodes[1].position = nodes[0].position - new Vector3(Metrics.NODE, 0f, 0f);
    68.                 nodes[2].position = nodes[0].position - new Vector3(Metrics.NODE * 8f, 0f, 0f);
    69.  
    70.                 break;
    71.  
    72.             case PlayerDirection.LEFT:
    73.  
    74.                 nodes[1].position = nodes[0].position + new Vector3(Metrics.NODE, 0f, 0f);
    75.                 nodes[2].position = nodes[0].position + new Vector3(Metrics.NODE * 8f, 0f, 0f);
    76.  
    77.                 break;
    78.  
    79.             case PlayerDirection.UP:
    80.  
    81.                 nodes[1].position = nodes[0].position - new Vector3(0, Metrics.NODE, 0f);
    82.                 nodes[2].position = nodes[0].position - new Vector3(0, Metrics.NODE * 8f, 0f);
    83.  
    84.                 break;
    85.  
    86.             case PlayerDirection.DOWN:
    87.  
    88.                 nodes[1].position = nodes[0].position + new Vector3(0, Metrics.NODE, 0f);
    89.                 nodes[2].position = nodes[0].position + new Vector3(0, Metrics.NODE * 8f, 0f);
    90.  
    91.                 break;
    92.         }
    93.     }
    94. }
    95.  
    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.
     
  2. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    Any ideas what’s wrong?
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.
     
  4. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    Screenshots of what exactly?
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.
     
    Joe-Censored likes this.
  6. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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. :p
     
  8. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     

    Attached Files:

  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It doesn't look like it disappeared. It looks like it is mostly inside the ball to the right.
     
    Schneider21 likes this.
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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?
     
    stain2319 and Joe-Censored like this.
  11. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     
  12. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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!
     
    stain2319 likes this.
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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!)
     
    Joe-Censored likes this.
  14. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    There's a similar game on Xbox already called Snake Pass, I think it was made in Unity.