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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

CameraFor2D

Discussion in 'Works In Progress - Archive' started by dkoontz, Feb 1, 2013.

  1. Dre_On_Unity

    Dre_On_Unity

    Joined:
    May 22, 2013
    Posts:
    16
    Well, it seems your "lookahead" feature is connected to your character controller, rather than to your camera. When I add your script to my hero, it doesn't work.
     
  2. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    The camera tries to center on whichever Transform you set on the camera's "Initial Target" field. It does not need a Character controller to function.

    Try going into the All In One example scene. Add the "Add Influence" script to the main camera. Walk down to the area with no camera bumpers. Adjust the X influence and you will see the camera move to the right of where the character is. Using influences in this way is one approach to doing lookahead. Again it depends on what the platform controller is trying to do. My guess is that it is directly controlling the camera, which is at odds with the CameraController2D as they don't know about each other and thus can't coordinate their actions.
     
  3. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    Hello, I tried out your demo and it doesn't seem to be working for me. It doesn't seem to be any locking/stopping, I am able to see a bit into the next room. But I was wondering if its possible to support 3.5.7 users (yes I know, late adapter here), and can the camera aspect ratio be any size?

    Thanks for your feedback!
     
  4. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    Hrm, I seem to have broken the demo scene, thanks for pointing that out.

    Yes any aspect ratio can be used, the system uses the camera's view area to determine when to stop scrolling so perspective works as well as ortho. Of course setting up your screen edges in 4:3 and then switching the camera to 16:9 will cause problems because you've got colliders saying "don't go past this point" inside your screen area. So I would definitely suggest designing for a single aspect ratio.
     
  5. jeremiasz

    jeremiasz

    Joined:
    Feb 17, 2013
    Posts:
    53
    Hello! I must say that CameraFor2D is quite cool. Is there any chance for Playmaker actions? CameraFor2D would be ultracool with that.
     
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    On your roadmap on the website you have:

    I was wondering if you had a chance to implement this feature?
     
  7. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    I did start messing with playmaker a few weeks ago. What actions would you like to see? I would assume things like setting a target, and perhaps zooming?
     
  8. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    So you can get this done currently by making your target be an empty GameObject (which is a good idea in general anyway) and them moving that ahead of your character based on momentum, etc.
     
  9. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    295
    Picked this up a while back, but have a question. Is there a way to stop all tracking with this when I don't want to use it? I call RemoveCurrentTarget() but then it tries to track to 0,0,0 - my work around is to just disable the entire script and re-enable it when I need it again, which seems a bit cludgy.

    Thanks!
    Jesse
     
  10. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Is it possible to have the Camera auto zoom in to fit within the open space created by the Camera Bumpers that are surrounding the room?

    I ask this because in our 2D game our rooms aren’t simply squares as they are in your demos. Sometimes we have L-shaped or C-shaped rooms that are connected by different shaped hallways. And sometimes the connecting hallways are more narrow than the camera, thus it would be great if somehow there was a way to zoom the camera in based off of the Camera Bumpers, so the player never sees the outer geometry that breaks the illusion.

    Currently, because of the cameras panning and the fact the camera does not zoom in when theres a transition from a room to the hallway, the player can see geometry that they are not suppose to see.
     
  11. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    Could you be more specific about what you mean by "stop all tracking"? You don't want the camera to go to (0, 0, 0) so clearly you want the camera pointing at *something*. Ideally you have a camera target that is independent of your player or whatever it is you are normally tracking. When you want to go into a fixed camera type mode you would have that object stop following your main object and instead hold still.
     
  12. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    How would the camera know to zoom in vs stopping you from moving through a wall as when you are moving around in a normal room? Currently you can adjust the zoom level on the CameraController and could do that based on a trigger at the edge of your room that smoothly transitions your camera from fully zoomed out as you want it in the room to fully zoomed in for the hallway. If you can provide me some examples of your room layout and where exactly you would want the camera to transition I could potentially give you more specific suggestions.
     
  13. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    431
    Hey David,

    I had a question:

    How can I prevent camera bumpers from being crossed by the camera?
    I'm finding that whenever the camera target goes too close or beyond the collider for a camera bumper, the camera begins to ignore the bumper, trying to reach the other side.

    How do I prevent the camera from passing the confines of the camera bumpers, by all means? (For example, if I wanted the player to go off-screen, without the camera following them beyond the camera bumper.) Basically, how can I remove the Megaman-Room-Change-style camera move transitions?

    Great asset, by the way. :D
     
    Last edited: Jan 14, 2014
  14. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    The short answer is, don't make your player your target. Make an empty game object that follows your player around (when that is what you want) and then holds still when you want the player to be able to move off screen.

    I'm not sure what you mean by remove the Megaman style room changes. In the all in 1 demo scene there is room by room transitions that happen because the camera bumpers are at the screen edges and there is an area where the camera freely pans around following the player. If you tell me more about your game I may be able to advise you a bit more.
     
  15. colinGravity

    colinGravity

    Joined:
    Sep 13, 2013
    Posts:
    2
    Hi David

    I decided to try out CameraFor2D because I'm trying to prototype a game that features two player objects that I always want in view, and had been trying to work out a way to get my orthographic camera to resize as they move apart or together.

    I'm very new at Unity, and I'm a designer, not a coder. I've been trying to learn a bit over the last month or so, but have been sticking to JavaScript. So please forgive my formidable ignorance. I can't work out how to AddTarget. I know I'm missing something very basic. Could you possibly explain how I would get CameraFor2D to just recognise 2 players all the time?
     
  16. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    To do this you would want to add both characters as a new target. This would require a small amount of code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. // Use this script to add multiple targets to your camera on start
    6. public class FocusMultipleTargets : MonoBehaviour {
    7.  
    8.     // The targets you want to be focused initially
    9.     public Transform[] targets;
    10.     // Optional, will default to the main camera
    11.     public Camera camera;
    12.  
    13.     void Start () {
    14.         if (camera == null) {
    15.             camera = Camera.main;
    16.         }
    17.  
    18.         var controller = camera.GetComponent<CameraController2D>();
    19.         controller.RemoveCurrentTarget();
    20.         controller.AddTarget(targets);
    21.     }
    22. }
    23.  
    I'll include this script in the next update.
     
  17. colinGravity

    colinGravity

    Joined:
    Sep 13, 2013
    Posts:
    2
    Thank you so much! I really thought I was just missing something obvious, I didn't expect you to have to do any coding. I really appreciate it.
     
  18. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    935
    Does this work with any camera? I'm interested in knowing if I could use this with the 2DToolkit cameras.
     
  19. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    I haven't used 2D Toolkit, but if their camera works by physically moving around (which I am sure it does) then I don't see why this wouldn't work. Do you know what plane the 2D Toolkit camera is looking at? I would assume it's the x/y plane which does definitely work.

    Edit: The only problem that might arise is if 2D Toolkit has something that tries to control the movement of the camera (which is doubtful). Obviously you couldn't use 2 different scripts that both try to control the camera.
     
    Last edited: Jan 17, 2014
  20. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    935
    That's cool. I was only in doubt because you mentioned that your script supports zoom and all, but on 2DToolkit cameras zoom is done in a different manner. Thanks for the clarification!
     
  21. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    Zoom is a good point. The way CameraFor2D does it is by adjusting the size value (when in ortho) or the actual distance from the target (when in perspective).
     
  22. neonyourself

    neonyourself

    Joined:
    Dec 12, 2013
    Posts:
    5
    Hi there, just purchased this today, and for the most part its going great. I have one feature that I'm not sure if its possible to do already so i've implemented a slight hack (see attached).

    Basically I want the user to be able to move the cam freely with touch (angry birds style) but also for it to track targets. My hack was to add an additional AppendTranslateForce() method that the user calls when they want to freely move the camera. This then shorts your ApplyMovement() method and calls my own ApplyTranslate() whilst its active, before returning to normal.

    My implementation seems to work ok in the editor but on my device (Nexus 7 tablet) it seems to be a little jittery. I was wondering if you were thinking of implementing something like this or perhaps its already possible and Im not seeing it? If not could you just have a quick check at my implementation to see if there is anything obvious.

    Regards and nice work

    Ash
     

    Attached Files:

  23. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    My approach would be to set up a target GameObject for the camera that normally follows the player. When you detect a swipe or whatever motion it is that goes into "free look" mode, I would then have that object stop following the player and instead pan with the movement. After some time with no input or perhaps triggered by a button (or neither), the target would move back to where the player is (or just require the player to pan back). That way you don't need to mess with anything inside the CameraController, you just move your GameObject around.
     
  24. neonyourself

    neonyourself

    Joined:
    Dec 12, 2013
    Posts:
    5
    that's an interesting thought. I managed to get the camera working more fluently by decreasing the swipe tolerance on my input so at least I have a workable solution atm. Still I think I'll look at your suggestion as it seems neater and im going to want the camera jumping around a bit and doing it by 'proxy' so to speak would be cleaner. Thanks for your input. Ash
     
  25. ChaneDev

    ChaneDev

    Joined:
    Feb 12, 2013
    Posts:
    66
    Does Push Box work for a perspective camera? It doesn't seem to for me. When I switch to a perspective camera the Push Box guides disappear.
     
  26. Mathius777

    Mathius777

    Joined:
    Jul 26, 2012
    Posts:
    32
    I just started using this asset, and am having issues with the "Height from target" property on the camera. I am setting it to "5", and when my game loads, there is a delay of when the camera actually moves above the player. So the player spawns in the center of the screen, then they appear to "move down" as the camera height moves up according to the setting. What would cause this kind of delay? I would like it to be instant. Thanks.
     
  27. hurleybird

    hurleybird

    Joined:
    Mar 4, 2013
    Posts:
    252
    Any way to have the camera follow the target's rotation as well as position?

    Edit: Actually, rotation was easy enough to hack in, but there's a larger problem. As far as I can tell, there's no way to give the camera an offset... it always wants to put the target object in the dead center of the screen. I can hack that in as well, and it's fine until it interacts with the hacked in rotation.

    Some of the features of this camera look awesome, but it seems to be designed with a specific kind of game in mind. At this stage modifying the camera to do the kind of stuff I'm looking for probably isn't worth the effort, and I'll have to go about building something more specific to my needs. Unless I'm missing something or this functionality can be quickly added in an update, could I get a refund?

    Edit again: Nevermind, figured out a really easy solution that I should have noticed if I had the patience to read through the thread earlier :p
     
    Last edited: Jun 6, 2014
  28. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    The camera doesn't modify or generally care about rotation so you could modify that yourself or use something like the Pathologic constraint system. Hopefully your exploration yielded the result you were looking for.

    :p
     
  29. RaymondR

    RaymondR

    Joined:
    Feb 14, 2014
    Posts:
    2
    Hi DKoontz,
    First this asset package is awesome! I'm really enjoying the camera script and it's very smooth which has been great.

    The short is I'm having trouble getting the 'focus point of interest' to work.

    I'm working in a 2D game and have developed my own player movement script. This all works great with the camera following the player. I've checked out the docs, video and example but I'm a bit stuck.

    The 'point of interest' script is not working for me, I have the script attached to an object and just have it target it's self. Also if I attached the player move script that the look ahead influence has no effect.

    Something I wondering if they have impact:
    - My scene is a 2D, does that matter?
    - I have played with the camera height well looking at my 2D scene in 3D, and I don't see any debug distance lines line in your video.
    - My player name is actually named 'hero' would that cause issues with influence? (guessing not because everything seems object target based )
     
  30. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    I assume you tried out the FocusPointOfInterest script from the demo scene? The target of that script should be your player. The way that script works is to add an influence to the CameraController when the target gets close enough.

    - 2d doesn't matter.
    - As for not seeing any debug lines, were you looking in the scene window or the game window? If you're in the game window make sure the Gizmos button is on.
    - Naming has no effect.
     
  31. RaymondR

    RaymondR

    Joined:
    Feb 14, 2014
    Posts:
    2
    ok so lame but it looks like i just didn't set the point of interest target correctly :) so that was a noob move :)

    Also I'm custom scripting the "lookhead" feature for touch on a mobile device, so far it's working okay. Tho touch movement is not as smooth as axis input.

    very cool plugin!
     
  32. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,457
    Very cool asset.
    Wondering if there are any new updates because the last was 14 months ago.
     
  33. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    rocki, I haven't updated the asset because I haven't been getting bug reports or feature requests that would necessitate it. I think everyone who's had an issue has resolved it here on the forums.
     
  34. rayfigs

    rayfigs

    Joined:
    Feb 8, 2009
    Posts:
    41
    I am in need of a 2d camera controller for a side scroller that would mimic tiny wings
    . It would seem that with milti focus I could do it but just checking here to confirm if your asset would be able to allow to to have such camera for my infinite runner?
     
  35. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    So for a TinyWings camera it seems like you'd want to do dynamic zooming more than multi-focus, perhaps you would need to do both at the same time to achieve some lookahead. Either way this camera system can do that.
     
  36. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    431
    Ack, CameraFor2D seems to be broken under Unity5. Any quick and easy fixes to get it up and running again...?

    EDIT: disregard, issue was on my end. CameraFor2D still going strong!
     
    Last edited: Mar 7, 2015
  37. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    Haha I was going to say I had tested that and it all seemed to be working. Glad to know you figured it out.
     
  38. ShadowLiu

    ShadowLiu

    Joined:
    Aug 30, 2012
    Posts:
    17
    In a perspective camera mode, i want to zoom in/out. The value of “Height from Target” has been changed,But it not work for camera's position. By the way,my version is 1.2.3 and Unity 5.0.1
     
    Last edited: May 21, 2015
  39. ShadowLiu

    ShadowLiu

    Joined:
    Aug 30, 2012
    Posts:
    17
    I got JIT error on iOS. Because of the method "IdealCameraPosition" used LINQ to get Max/Min value of enumeration. How can i fixed that ?
    attach on image

    Updated:I used UniLinq to replace System.linq library. it's work now
    https://github.com/RyotaMurohoshi/UniLinq
     

    Attached Files:

    Last edited: May 29, 2015
  40. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    Well to be fair, when doenloading the plugin on a fresh unity 5.0.2 project you get these 2 errors..

    Error 1 :
    Assets/CameraFor2D/Example/Scripts/KeepFocusedTargetsOnscreen.cs(42,62): error CS0619: `UnityEngine.Component.camera' is obsolete: `Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)'

    Error 2:
    Assets/CameraFor2D/Example/Scripts/KeepFocusedTargetsOnscreen.cs(42,69): error CS1061: Type `UnityEngine.Component' does not contain a definition for `WorldToViewportPoint' and no extension method `WorldToViewportPoint' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)


    But they are both in the example folder, so i guess you can just delete the examples folder and continue :p
    OR - Right click the script and click "Run api upgrader" :D also fixes it.
     
  41. Cactuar18

    Cactuar18

    Joined:
    Jun 29, 2013
    Posts:
    3
  42. tequyla

    tequyla

    Joined:
    Jul 22, 2012
    Posts:
    335
    Hello,

    is possible to update the script for last version of unity ?

    +++