Search Unity

I am very new to this unity tool. need some help

Discussion in 'Editor & General Support' started by suresh, Mar 29, 2011.

  1. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Hi people

    Is it possible to develop the snake game in unity,if so can any one tell me how. Please help out in this regards. It will be very helpful to me in my work.
     
  2. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    What do you mean by that.... do you want to know how to develop game or how to develop snake game....?
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Yes. Well, a snake game (not sure what you mean by 'the' snake game).

    Probably not in a single post :) Also, there's no one answer to that question (there are many ways it could be done).

    What do you need help with exactly?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,717
    Start by reading the manual.
     
  5. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    my problem is how to create the surface and how to create the snake and how to coordinate them in play
     
  6. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Learn about Unity Interface,Basic Scripting.... from the unity tutorial page or some pdfs.....
     
  7. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    @Dinesh Balu,,,,,can you give links to some good materials.....
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,717
  9. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    to create the snake game we need to have some surface for the snake to roll on. Is it the right process to create that surface with terrain? or do we need use some thing else to use? and what we have to use to create snake? please reply to his post.
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    There's not really any 'need to' involved. There are really no rules or guidelines about how such a game should be created. And, there's no particular need to have a 'surface' (conceptual or actual) for the game to take place on. (Unless, of course, having such a surface is specified as part of the design.)

    That would be unusual, but might be an option if you wanted to make a physics-based snake game of some sort (or even a 'typical' snake game, but where the snake followed the contours of the terrain as it moved). But in general, no, you don't have to use a terrain.

    Could be a 2-d array, or a queue, or a container of game objects, or multiple rigid bodies linked together, or one game object and a procedural mesh, etc., etc. In other words, there's nothing you 'have' to use. There's a multitude of ways to do it, so your best bet is probably just to pick a method and go with it.

    Ok :)
     
  11. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Thanks a lot for your reply, I made the snake floor with game object and plane(mesh filter) and i added mesh renderer to them. And i am wondering how to make a snake, is there any compound to make or do we need to write script to make the snake.And what about the snake food and and snake tail.
     
  12. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Generally speaking, you'll need to write code for all that.

    At its simplest, a 'snake' game can be implemented using a 2-d array, where every cell is either open, a wall, occupied by the snake, or occupied by a 'food' item. One approach would be to use individual game objects for the walls, food items, and the snake body, and have the cells be GameObject references that are either null (unoccupied) or reference an appropriate object.
     
  13. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    i am unable to proceed further in this game development, I am getting confused with the creation of the snake.
     
  14. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Try to learn each concept of the unity game objects and scripts. then start on a game....
     
  15. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Like Dinesh Balu said, you'll need to learn the basics before you can make something of the complexity of a snake game. Specifically, you'll need to learn about game objects and components and how they relate to one another, and you'll need to learn at least the basics of programming in one of the languages that Unity supports.

    You can get help here, but you'll need to ask specific questions. In other words, instead of just stating that you're not able to create the snake for your game, you might say, 'I tried to assign my script to a game object, but got this error' (for example).
     
  16. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    All about snake game. I did some thing like this

    1. I have created a game object cube(floor) and i added rigid body to it and i put is kinematic checked
    2. I have created another cube and i made this snake(cube) as the child to the floor(cube) and then
    3. I have added script to this snake(cube) the script.

    Is this process is right one to make the snake game.??
    will be waiting for suggestions.Thanks in advance.
     
  17. Lioncirth

    Lioncirth

    Joined:
    Apr 7, 2009
    Posts:
    111
    I would recommend checking out Tornado Twins on YouTube. They have done a worms game, you could follow this tutorial and it may help with what your after.
     
  18. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Is there a particular reason you've made the snake object a child of the floor object?

    Also, if you're making a typical grid-based snake game, I wouldn't bother using the physics system. (If you're making a physics-based game, then using the physics system is appropriate, of course.)
     
  19. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    thanks for the reply @lioncirth @Jesse Anders, I am in learning stages of gaming, so i don't know what is appropriate, I am just trying the things randomly to work it out. can you tell me that is the right way. If that is not the right way please tell me the way.
     
  20. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Doing it that way might be appropriate in some circumstances, but unless you have a particular reason for doing so, I wouldn't make the objects in the game child objects of the floor.
     
  21. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    can any one please tell me, what is the java script for the food (to come in random positions), and when snake touches the food the snake should grow.
     
  22. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    There is no 'the JavaScript' for food or making the snake grow. Programming is kind of like writing; there are many ways the same idea can be expressed, and there's rarely a single 'right way' to do it. Also, how to do it depends on many things, such as how your game world is represented, whether you're using the built-in physics system or handling collision detection and response yourself, and so on.

    For creating food items at random locations, look at Instantiate() (to create the food items) and Random.Range() (to generate a random location). As for having the snake pick up the food, that depends on how the snake is represented and whether you're using the built-in physics system.
     
  23. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I have represented the snake as a rigid body and i have checked the kinematics in it. Still when it hits something its going in side that its not stopping there.
     
  24. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    It won't do automatically... you should add some code on detecting collision....
     
  25. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Can you post the entire script? (Also, be sure to use 'code' tags when posting code.)
     
  26. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    still i am writing the script is this the right way? can you tell me how to deal with the food one?
     
  27. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I am able to generate the clone of the food, after the first food destruction the second one is creating but i am unable to destroy that one. Can any one tell me how to do so.
     
  28. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Even if you're still working on it, please post the entire script. That will make it easier for us to help.
     
  29. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I am totally new to this unity and scripting,

    I have complete my project by this week end... I don't know what to do...I am working on and on..... can you please help me out in this
     
  30. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Please repost all your code using 'code' tags.

    If you don't know what that means, just ask, and I or someone else will be happy to help.
     
  31. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Closer, but you're using the wrong tags :)

    You used 'quote' tags, but for code, you should use 'code' tags.

    Using the 'editor' buttons, 'quote' tags are added by the little button that looks like a speech balloon, and 'code' tags are added by the button that's a number or 'pound' sign. So you were very close :)

    There's not much code there, and it'd certainly be easy enough to help with the code as it is now. But, putting the code in 'code' tags is very easy, so there's really no reason not to do it. And, even if it makes the code just a *little* easier to read (which it will), it'll make it easier for people to help you.
     
  32. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    this is for snake movements and collision detection

    Code (csharp):
    1.  
    2. var speed = 5.0;
    3. var food : Transform;
    4. var snake : Transform;
    5.  
    6. function Update ()
    7. {
    8. //var controller : CharacterController = GetComponent(CharacterController);
    9. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    10. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    11. transform.Translate(x, 0, z);
    12.  
    13. //Instantiate( Snake, Vector3( Random.value * spawnGridSizeX, Random.value * spawnGridSizeY, Random.value * spawnGridSizeZ ), Quaternion.identity );
    14. }
    15.  
    16. function OnCollisionEnter(theCollision : Collision)
    17. {
    18.  
    19.  if(theCollision.gameObject.name == "Food")
    20.  {
    21.  //for(var i : int =0; i<3 ; i++)
    22.  //{
    23.   Debug.Log("Hit the Food");
    24.   Destroy(gameObject.Find("Food"));
    25.   //var position = Vector3(Random.Range(-2,2), 0, Random.Range(-3, 3));
    26.     Instantiate(food, transform.position, transform.rotation);
    27.  
    28.   //var position = Vector3(Random.Range(-36,36), 0 ,Random.Range(-23, 23));
    29.   //Instantiate(food, position, Quaternion.identity);
    30.   }
    31.  
    32.  
    33.   else if(theCollision.gameObject.name == "Wall")
    34.   {
    35.     Debug.Log("Hit the wall");
    36.   //  This is to destroy the object after the colliosion
    37.   Destroy(gameObject);
    38.   //Instantiate(snake, transform.position, transform.rotation);
    39.  
    40. }
    41.   }
    42.  
     
    Last edited: Apr 6, 2011
  33. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    @Jesse Anders, Is this the right way, help me out in solving this snake game......my professor is really horrible....
     
  34. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, there's one more thing you need to do. Sometimes when code is posted to the forums, the indentation is removed. I'm not entirely sure what causes this, but I think if you do the following the problem will be fixed:

    In your code editor, replace all tabs with spaces. Then, repost your code. The indentation should then be preserved.

    The reason it's important to preserve the indentation is that with many programming languages, indentation is an important visual cue that helps make the structure of the code clear. Without the indentation, it can be slow and tedious to try and figure out the logical flow of the code.

    I can't guarantee I'll be able to help once you get the code posted correctly, but I'll certainly take a look at it at least. And, if you can get the code posted in an easy-to-read format, others will probably be more likely to help as well.
     
  35. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I did that way only, isn't it clear enough to read?
     
  36. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    If you're not sure how to fix the indentation, is there someone you could ask for help on that?

    What code editor are you using? If it's one I'm familiar with, I'll explain how to do it.
     
  37. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    no one is there to ask, my code editor is UniScite.
     
  38. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, I'm not sure what causes indentation to disappear completely, but here's how to replace tabs with spaces in UniSciTE. (If you want to keep your source files the way they are, you might do this with a copy of them.)

    1. Open the file, and then press ctrl-H to open the 'search and replace' dialog.

    2. Check the button at the bottom that says 'Transform backslash expressions'.

    3. In the 'Find what' box, enter '\t' (not the single quotes, just the backslash and the t).

    4. In the 'Replace with' box, enter four spaces.

    5. Click 'Replace All'.

    I can't absolutely guarantee this'll work, but give it a try. Replace tabs with spaces in one of your files (or a copy of it), and then post that code to the thread using 'code' tags. With any luck, the indentation will then be preserved.
     
  39. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    yes i did it now is it ok.....i replaced it
     
  40. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    this is for snake movements and collision detection

    Code (csharp):
    1.  
    2. var speed = 5.0;
    3. var food : Transform;
    4. var snake : Transform;
    5.  
    6. function Update ()
    7. {
    8. //var controller : CharacterController = GetComponent(CharacterController);
    9. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    10. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    11. transform.Translate(x, 0, z);
    12.  
    13. //Instantiate( Snake, Vector3( Random.value * spawnGridSizeX, Random.value * spawnGridSizeY, Random.value * spawnGridSizeZ ), Quaternion.identity );
    14. }
    15.  
    16. function OnCollisionEnter(theCollision : Collision)
    17. {
    18.  
    19.  if(theCollision.gameObject.name == "Food")
    20.  {
    21.  //for(var i : int =0; i<3 ; i++)
    22.  //{
    23.   Debug.Log("Hit the Food");
    24.   Destroy(gameObject.Find("Food"));
    25.   //var position = Vector3(Random.Range(-2,2), 0, Random.Range(-3, 3));
    26.     Instantiate(food, transform.position, transform.rotation);
    27.  
    28.   //var position = Vector3(Random.Range(-36,36), 0 ,Random.Range(-23, 23));
    29.   //Instantiate(food, position, Quaternion.identity);
    30.   }
    31.  
    32.  
    33.   else if(theCollision.gameObject.name == "Wall")
    34.   {
    35.     Debug.Log("Hit the wall");
    36.   //  This is to destroy the object after the colliosion
    37.   Destroy(gameObject);
    38.   //Instantiate(snake, transform.position, transform.rotation);
    39.  
    40. }
    41.   }
    42.  
     
  41. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Well, the formatting still isn't correct. But, at least you gave it a go, so I cleaned up the code to make it easier to read:

    Code (csharp):
    1. var speed = 5.0;
    2. var food : Transform;
    3. var snake : Transform;
    4.  
    5. function Update ()
    6. {
    7.     var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    8.     var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    9.     transform.Translate(x, 0, z);
    10. }
    11.  
    12. function OnCollisionEnter(theCollision : Collision)
    13. {
    14.     if (theCollision.gameObject.name == "Food")
    15.     {
    16.         Debug.Log("Hit the Food");
    17.         Destroy(gameObject.Find("Food"));
    18.         Instantiate(food, transform.position, transform.rotation);
    19.     }
    20.     else if (theCollision.gameObject.name == "Wall")
    21.     {
    22.         Debug.Log("Hit the wall");
    23.         Destroy(gameObject);
    24.     }
    25. }
    I removed all the commented stuff so that it's easier to see what code is actually being executed.

    A couple of things I noticed. In this line:

    Code (csharp):
    1. Destroy(gameObject.Find("Food"));
    You don't need to search for the game object, because you already have access to it. Instead, you can just write:

    Code (csharp):
    1. Destroy(theCollision.gameObject);
    Also, in this line:

    Code (csharp):
    1. Instantiate(food, transform.position, transform.rotation);
    You're instantiating the new 'food' object right on top of the player (or so it appears), which I doubt is what you want. (You mentioned instantiating randomly earlier, but I'm not sure which code you're actually using for this.)
     
  42. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    When i try to Instantiate the snake
    i am using this line to Instantiate, when the snake dead

    var snake : Transform;
    Instantiate(snake, transform.position, transform.rotation);

    its giving the following error,


    UnassignedReferenceException: The variable snake of 'Snakebehaviour' has not been assigned.
    You probably need to assign the snake variable of the Snakebehaviour script in the inspector.

    can any one tell me how to solve it
     
  43. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Actually i want to Instantiate the food randomly on the floor one after the one dead, but when i use the random code its creating food infinity, more over when the snake hit the wall it is dieing but i am unable to re create it.
     
  44. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    When i try to Instantiate the snake
    i am using this line to Instantiate, when the snake dead
    Code (csharp):
    1.  
    2. var snake : Transform;
    3. Instantiate(snake, transform.position, transform.rotation);
    4.  
    its giving the following error,


    UnassignedReferenceException: The variable snake of 'Snakebehaviour' has not been assigned.
    You probably need to assign the snake variable of the Snakebehaviour script in the inspector.

    can any one tell me how to solve it
     
  45. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Do you have anything assigned to that variable?

    Can you post that code again? (If you can remove any commented code and fix up the indentation before posting, that would help.)
     
  46. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Try approaching it one problem at a time (e.g. just focus on the 'food' problem for now).

    I don't see anything obviously wrong with the code that instantiates the food. In what way is it not working?
     
  47. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    thats really cool idea,



    Code (csharp):
    1.  
    2. var food : Transform;
    3. var position = Vector3(Random.Range(-10,10), 0 ,Random.Range(-10, 10));
    4. Instantiate(food, position, Quaternion.identity);
    5.  
    It created the object named Food(Clone) IN THE HIERARCHY, after the distraction of Food. But I am unable to see that on the floor. That is the problem.
     
    Last edited: Apr 6, 2011
  48. niosop2

    niosop2

    Joined:
    Jul 23, 2009
    Posts:
    1,059
    guessing the gameObject.name != "Food". Try using tags instead, and ensuring the prefab has the tag set on the food gameObject.
     
  49. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Thanks for your reply

    where should i put this

    yes i did it, but still....
     
  50. niosop2

    niosop2

    Joined:
    Jul 23, 2009
    Posts:
    1,059
    Instead of

    Code (csharp):
    1.  
    2.  if(theCollision.gameObject.name == "Food")
    3.  
    use

    Code (csharp):
    1.  
    2.  if(theCollision.gameObject.tag == "Food")
    3.  
    and make sure the game object in the prefab has a tag of Food