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

Parallax Strategies?

Discussion in 'Scripting' started by Nadesican, Jun 20, 2016.

  1. Nadesican

    Nadesican

    Joined:
    Sep 8, 2013
    Posts:
    3
    Hey all! I'm working on my first completely solo game project, a 2D runner. It currently spawns the land the player is running on (and a random selection of obstacles) based upon the land's width and distance from a generation point, placed decently off camera.

    This worked relatively well until I attempted to add a second, background layer. The background was moving at an equal rate to the player - not something I wanted. How should I fix this? My first attempt was to give it a rigidbody and have it move in time with the player, at a slower rate. This worked.. but also slowed processing speed to a crawl, presumably because all the backgrounds were player controlled.

    My goal here is to have the background function as another layer containing distant enemies (towers) who lob attacks at the player.

    Thanks in advance guys!
     
  2. listener

    listener

    Joined:
    Apr 2, 2012
    Posts:
    179
    There are many ways of achieving parallax effect simplest would be:

    1. Make a script that holds list of objects ( that you are gonna move, clouds, mountains etc.)
    2. Define spawn point (where items will be positioned from start also when they reach remove point, off screen of course)
    3. Define remove point (when item reaches this point you just move it to spawn point)
    4. You can move items at constant speed and use multiple scripts for more layers or each time you move object to spawn point you generate some random speed and move it at that speed until it reaches remove point (Keep this speeds in list that corresponds to the one with items that you move.)

    This is really simple method from here you can further complicate things as much as you want.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Nadesican

    Nadesican

    Joined:
    Sep 8, 2013
    Posts:
    3

    This is essentially what I have for the foreground, and it works fine. The problem is that I have the camera set to follow the player, rather then run on a rail - my plan is the introduce skill based platforming with an advancing 'Death Wall' to keep players moving forward. This means that the usual methods with a fixed camera don't work, or at least not in a way I've thought of yet. Attempting this method with the background has so far only managed to slow my game into slideshow mode.

    I'm considering the 'texture rotation on a quad' method at the moment, as only one or two things need to ever be generated on the background level, and I can just chain that to the camera.