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

Is it possible to hard code scroll lock on an empty game object?

Discussion in 'Scripting' started by SuperCrow2, Jun 16, 2019.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    and then placing said object in the screen so when the camera gets to the object, the screen stops moving? The idea is all I need to do is place the empty game object wherever I want the screen to stop moving.

    Because I was thinking about the screen scrolling and any game I make where I want the camera to stop following the player at certain parts will take me forever to finish making the game.

    How will that code look like using Csharp?
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Perhaps you can tell us a little bit more about what you actually mean by topping the screen to move, and screen scrolling. Perhaps show us how you are currently doing this, and we can then talk about how we could expand this idea to a generalized method. Roght now, I'm afraid I don't understand what you are trying to do and why you would do that. Also, are you talking about a 2D or a 3D game? Since you seemingly use 'moving' and 'scrolling' interchangeably, I believe you may be developing a 2D scroller, but it is not clear to me.
     
    SparrowGS likes this.
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    2D game.

    The camera moves with the character but at certain parts in the levels I want the camera to stop moving when the camera boundaries touches the boundaries of my empty game object.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    This should be quite doable. In order to make this work, you should define two conitions in a General way:
    1. Stop-Condition: when should the camera stop following the Player object? I believe this could be implemented quite easily: issue a Special tag to the 'stopper object' (that moves with the world) and add a collider. Whenever the camera's collider Registers that it collides with an object that has the stopper tag, make it stop following the Player (perhaps use OnCollisionEnter, better yet, use it as Trigger with OnTriggerEnter).
    2. Resume Condition: when should the camera resume following the Player. Part of this will usually contain some cleanup code so that the object that triggers the stop will not inconveniently re-trigger as soon as you resume. Carefully map out how you Transition from 'stopped' to 'following'.
    After that all that remains is placing the 'stopper objects' with your Scene, and add the script and collider to your camera that controls Player following.