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

Basic Scrolling?

Discussion in '2D' started by EcComicFan, Sep 24, 2016.

  1. EcComicFan

    EcComicFan

    Joined:
    Sep 11, 2016
    Posts:
    7
    Hi all,

    First off, I'm new here so sorry if this is the wrong place to ask this. Started learning Unity about a month ago and I'm still having trouble with even the most basic things it seems like. I'm trying to prototype an extremely simple game (my first original) in which the level is pre-built (not procedurally generated) along the y-axis and the player character as well as the camera are scrolling upward (also along the y-axis) at a constant speed, ideally at the same speed. Think almost like an airplane that is able to move left and right but not up and down.

    The trouble I'm having is a can't figure how to just make my sprite character scroll upward along the y-axis. I also haven't figured out how to make the Main Camera scroll either. I know how to make the move using inputs, but having it happen automatically is beyond me at this point.

    I assume if it were a 3d game I'd be using Vector3's and transform.position to move them (concepts I'm just barely beginning to understand), but they don't seem to apply here.

    I completed the Roll-A-Ball, Space Shooter, Basic Scripting, and 2d Game Creation tutorials, but I guess I haven't really learned anything at this point. Any explanation as to how to make this work and why the script works would be extremely appreciated as, so far, it feels like all the studying I've done up till now was pointless.
     
  2. EcComicFan

    EcComicFan

    Joined:
    Sep 11, 2016
    Posts:
    7
    Okay, so I figured out a solution. Perhaps you all can tell me if it's the best one. I attached this script to both the player and camera components:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraScroll : MonoBehaviour {
    5.  
    6.     public float scrollSpeed;
    7.  
    8.  
    9.     void Start ()
    10.     {
    11.     }
    12.  
    13.     void LateUpdate ()
    14.     {
    15.         float yPos = transform.position.y;
    16.         transform.Translate(new Vector3(0.0f, scrollSpeed, 0.0f));
    17.     }
    18. }
    19.  

    Now, however, I'm encountering a whole new problem (the one I stopped working on before I decided to try and solve the scrolling issue). In the video below you can see that I'm trying to have my player character's tail act like a chain using hinge joint 2d. The idea is that is almost flows in the wind and swings with momentum. Once the character begins moving, however, the links pull apart slightly and the momentum isn't right at all.



    I tried adding distance joints but they continued to pull apart and, worse yet, started stuttering around and bugging out. Any ideas?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, the problem is that you're trying to use joints, which are part of the physics engine. And that forces you to use physics for everything. In particular, your scrolling code, which would be quite correct if you weren't using physics, bypasses the physics engine, which makes it angry and then it finds ways to punish you.

    For your first simple game, which I think I can totally picture (there were some classic arcade games that worked the same way), you really shouldn't be using the physics engine. You'll still be able to code a trail if you want, and in fact I think you'll be able to do it better without physics than with it. So, throw that out, delete any Rigidbody components from your game objects, and get on with making a cool game!

    Best,
    - Joe

    P.S. OK, two minor corrections on your script. First, I recommend using Update instead of LateUpdate. Second, your line 15 isn't doing anything, and should be removed so it doesn't confuse you in the future. :)
     
  4. EcComicFan

    EcComicFan

    Joined:
    Sep 11, 2016
    Posts:
    7

    Thanks so much for the reply. Will definitely make the changes with my script as per your suggestion. Just out of curiosity, any suggestions on how would I begin to code the tail to have momentum without using physics joints? Almost all the tutorial work and studying I've done thus far has been about colliders, trigger events, and whatnot so I'm not really sure where to begin with assembling the tail via code, as opposed to jointing it together.

    (Also, I'm extremely happy you made the arcade observation as my main goal is to make a modern version of an early eighties arcade game. Brownie points for you!)
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Sure, it's unfortunate that so many newbie tutorials rely on physics, because that really is a much harder way to go than no-physics, particularly for 80s style arcade games (which did not have physics engines).

    So the basic concept is: you want a sprite to move a certain way? Write code to move it that way. :)

    In the case of a smoke trail, it will be composed of individual sprites (smoke puffs), each of which will have a script (SmokePuff) on it. Now just think about how you want your smoke puffs to behave. Most likely, you want them to either stay put (which will make them fall behind you at the same rate you're moving forward), or move backwards at some speed. So write a bit of code (much like your scrolling code above!) that does exactly that. Then you may want to add a bit more code that scales them up, and fades them out (and eventually they delete themselves, when offscreen or completely faded out). Now you've really got a great-looking smoke trail, and it took only a few lines of code to make it look exactly the way you want!
     
    theANMATOR2b likes this.