Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scripting a train game

Discussion in 'Scripting' started by l_reg, Jul 8, 2014.

  1. l_reg

    l_reg

    Joined:
    Jul 8, 2014
    Posts:
    2
    Hello! I'm completely new to Unity, so please forgive any ignorant errors/etc I may make.

    In preparation for a course on Unity scripting, I've been tasked with building a simple, top-down, tiled train game. The goal of the game is to collect gems that pop up at random on the tracks. The position of the trains is predetermined, as is their speed, the time limit, frequency of gems, etc. Some tracks can be toggled between left/right/forward, but other than that the user has no control. The entire game, aside from a light source, game manager, and camera, must be scripted.

    So far the biggest problem I've run into is how to make the trains move and follow the tracks. I can get as far as setting up the board--just a bunch of plane objects with textures--but I have no idea how to actually make the game function...

    I've been looking online for ways to go about this, but either it's something that should be so obvious that no one's asking, or I'm looking in the wrong places. If someone could help me out or point me in the right direction, I would really appreciate it!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    How about a waypoint system?
    A train moves towards a waypoint, when it reaches it, the 'next' waypoint is selected, and the train moves towards that. If theres a junction, you have several possible 'next' waypoints to choose from.
     
  3. l_reg

    l_reg

    Joined:
    Jul 8, 2014
    Posts:
    2
    That sounds good! One question: if I were to set up one "chain" of waypoints along the track, would it be possible to have trains going in opposite directions? Or in other words, can waypoints lead to different waypoints based on the direction they're approached from?

    I guess I could also have two separate waypoint "tracks"--one for trains going one direction and one for trains going the opposite direction, but if it's possible to only use one track, that'd be nice.

    Thanks for your response!
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I can see you have this idea clear in your head. Let me give you a simple piece of advice... Go into Photoshop, put a bunch of blocks and points down and draw out a simple board to play on. This way, you can express exactly what you want to happen and gain complete understanding of what you want to do.

    Consider that all tracks go east/west. Using this, you create a waypoint cloud. On this cloud, you have 8 connectors. N, NW, W, SW, S, SE, E, NE. Each of those connectors either does, or doesnt have a point attached to it.

    Consider this map...
    trains.jpg

    Greens are E/W tracks. So if the train is traveling east, All it simply has to do is ask the node that it is at, what is the next East Node. Thus creating a waypoint track. At each point, the train could turn NE or SE. To do this, all you would have to do is ask if and what the NE/SE point is... giving you your next waypoint.
     
  5. sidharthubss1911

    sidharthubss1911

    Joined:
    Jan 1, 2016
    Posts:
    1
    you can always use simple waypoint system(SWS) which is available in asset store and in torrent
     
  6. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    You need to think about it in code. The waypoint system you create can do whatever you're capable of designing it to do. Let's say you have a train traveling east, and you know this train is traveling east, you can use that information to determine what the waypoint does when it's toggled.

    It won't matter what direction the train is traveling in as long as you account for the possible directions. As mentioned, open up paint or something and doodle your waypoint system out so you can understand what you need to account for.

    And did you just seriously suggest someone steal an asset that's available for $15 on the Asset Store?