Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trouble making puzzle game

Discussion in 'General Discussion' started by jtehlert, Mar 11, 2012.

  1. jtehlert

    jtehlert

    Joined:
    Feb 22, 2012
    Posts:
    21
    I am trying to create a simple puzzle game, but I am having problems with how I can accomplish some of the aspects of the game that I want to include.

    The idea of the game is that the player is a ball, and the puzzles are made up of cubes. When the player hits different colored cubes, things happen on the map. So if the player hits a red cube, a different cube on the map will move and open up a pathway. But on each level the triggers will cause something different to happen, so that no level is exactly the same.

    When I made a prototype for this game in UDK, it was really easy because I could set up triggers, and matinee sequences. So when the player hit that trigger, it played a matinee, thus moving any object that I wanted.

    But now that I am making the game in Unity, I am having difficulty setting up a way to accomplish this. I have been trying to figure out an efficient method of setting up the triggers to do different things in each level (to solve the different puzzles). What I am thinking I may have to do is write a script for each level and apply it to an empty gameObject in the level and then set up all of the triggers and movements in that script. But if I do that, is there a way to check collision between two objects, without that script being on either object?

    If you guys know of an easier way of doing this, it would be greatly appreciated!
     
  2. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    If I understand you correctly this is what I would do. You're on the right track with having a "main" script attached to an empty game object. You'll need some kind of script for the cubes, to send a message to the main script there was a collision. You can put the logic in the main script with some branches that execute different behavior depending on the situation.
     
  3. jtehlert

    jtehlert

    Joined:
    Feb 22, 2012
    Posts:
    21
    So if I wanted to have 4 RedCubes on the map (each with a different name) and have just one script applied to the RedCube prefab, could I set up a variable that got the name of the current RedCube being collided, and then send that variable to the main script for checking?
     
  4. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    Yes you could, there is a lot of flexibility in this.
     
  5. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Honestly, I would make each of the trigger objects send an event and each of the triggered objects either listen for those events or have one object which acts as a controller listen for the events and then instruct the appropriate object to act.
     
  6. jtehlert

    jtehlert

    Joined:
    Feb 22, 2012
    Posts:
    21
    I am trying to avoid having a new script on each object, trying to keep the project view as simple as possible. But your method does seem very similar to mine.
     
  7. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    Well you certainly don't want a different script for each object as you'd have to make all communication between your controller and object custom. As far as I know you can only reference a script by its exact name so that would be horrible. All you need is a generic script with a public integer that you assign during instantiation to differentiate between cube instances. You could create an array of cubes and make the cube integer the same as the index in the array which is really handy for all kinds of tasks.

    I would put only the bare minimum of logic on the cube scripts, if you split up the logic between the controller and cube you may end up having to do a lot of duplication, communication, creating spaghetti logic which can be confusing.