Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[Released] 2D Platform Controller

Discussion in 'Assets and Asset Store' started by JohnnyA, Mar 11, 2013.

  1. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Thanks for the info.

    For ledge grab you would need to have seperate colliders for the edges, not one big collider, as it uses the bounds to work out where the ledge is! I did start working on my terrain creator but maybe for now I'll have a chat to the creator of the asset and see if they can add ledge climb support for 2DPC.
     
  2. Seven

    Seven

    Joined:
    Apr 23, 2012
    Posts:
    111
    I did wonder if it would be something like that. It literally teleports to the edge of where the terrain would be if the section was rectangle. Creator is very approachable from what I've seen so far, would be awesome if you guys could get them playing flawlessly :D
     
  3. Toad

    Toad

    Joined:
    Aug 14, 2010
    Posts:
    297
    I've been looking at Ferr2DTerrain recently and it does look very cool. Would be great if it played nicely with 2d platform controller!
     
  4. sefou

    sefou

    Joined:
    Aug 30, 2011
    Posts:
    287
    Yep,
    i confirm , Ferr2D work perfectly with 2DPC.
     
  5. StephanM

    StephanM

    Joined:
    Aug 20, 2013
    Posts:
    25
    Thanks Jonnhy! And thanks to everyone else for the Ferr2D info. Excellent!
     
  6. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,166
    Since 4.3 is out now, what are the future plans on 2D PC?
     
  7. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    201
    I have the same question!
    I was just about to buy this addon but I want to have confirmation it will work with the new Unity 2D stuff, but I guess Johnny is already on it ;) ? I guess the Collider2D integration would be a thing that comes to mind?
     
  8. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    A native sprite animator and integration with 2D colliders and physics will be included in the next major release. Hopefully within a week or two but thats not a guarantee at this stage, not sure how different the new stuff is yet.
     
  9. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Hello JohnnyA, do you know by chance of the Platform Controller changes the parent of the GameObject with a script of your solution attached to it?
     
  10. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Yes it does. If a platform returns parentOnStand = true then the controller will be parented to it. This is used for moving platforms, ropes, etc.

    EDIT: To be clear it changes what it is parented to, it doesn't change values or transforms of the parent.
     
  11. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    882
    i have big overhead in my scene but i thinks is somethinks else i don't found where is can from
     
  12. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Ah okay, then I think I understand, because the controller is attached to an Object which is instantiated with it by his parent. It's doesn't have an influence on my game as of yet, but it changes the hierarchy, so I was like eh, this isn't something I've done haha :p Now I know, thanks :)
     
  13. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Send me the project if you can, I'll take a look.
     
  14. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,043
    Just a general question/request for discussion. I haven't played around with this or looked into it. Controller support? Anything special to know or think about? I'm working on a new game using 2D Platform Controller for Windows/MacOS and hoping to support keyboard and controller inputs.
     
  15. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    The basic idea is that you write your own input class which sets the values of X, Y, jumpHeld, etc based on the controller buttons. I have some samples (PS3 and Xbox controller for Mac), but they aren't robust enough for me to release (for example haven't tested on Windows). If you google search this thread you should see some links/code/etc.

    It really is pretty trivial to hook the input up, the issue is coping with different controllers, computers, etc. Another option would be to get a specialised controller package from the store and just map its inputs to the controller.
     
  16. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,043
    Hmm... I'll have to play around and see what I can figure out. I've never done anything but mobile apps with touch controls. I wasn't expecting controller support to be so complicated in Unity - I always figured it was something that "Just Worked". No such luck :(
     
  17. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    45 minutes later using the new 2D:


    The main problem in taking this to release is simply that everything has new names (RaycastHit2D, Collider2D, etc) and also slightly different signatures). So I'll need to add a layer of indirection between the classes and the behaviour, or else you wont be able to use the same code in both 2D and 3D. The issue here is that creating some new object each time we do a collision will be slow and memory hungry so it might take a while to find a nice solution.

    What I might do in the short term is provide a separate set of classes for working with 2D colliders.

    This doesn't mean you can't use the new sprite system and animation, this will cause no difficulty, took me 2 minutes to integrate with the simple animator they have in the sample. I'll aim to have a nice animator submitted by the end of the weekend.
     
  18. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Hey JohnnyA, it's me again :D I would like to implement Dashing in my game, would it take long tp get it working? I am using 2D Toolkit and your 2D Platform Controller, changing animation and stuff won't be hard, detecting the dash state would be harder I guess :eek:

    What I've done already is entered a new CharacterState between running and sliding, called DASHING, with the priority value 25.
    I am still kinda new to programming so I am a bit scared to change your code haha. Though I've accomplished alot in the four weeks I've been programming, I even created a multiplayer which syncs the armor currently wearing etc. But that's all because I knew what had to be changed to make it working. This is a different story because I didn't create the controller myself. So any tips which modifications it would take? Line numbers only are okay too. :)

    Thanks beforehand and keep up the good work. You've done a great job on the controller :D
     
  19. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Firstly do you have walk, run, and dash, or just walk and dash?

    If its the later you should be able to easily implement dash as run. All you would have to do is add a bit of code to your input controller which I can help you with.
     
  20. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Ah, well I can show you my code via Skype or whatever haha :p But the problem is I use Walk, Run and like to implement Dash, made a few changes already. Currently I am not using sliding but I guess that would be a lot of modification to the code because it changes the raycasts and stuff, so maybe it's better if I copy the Run statements and change them as needed? And I get the error The label `case 25:' already occurs in this switch statement at the moment so while I wait for your reply I am gonna solve that one haha :p Though I changed the value from case 24 to 25 and it just said the above error. When it was 24 it said it about case24:..

    Edit:
    Lol it was a typo, should have been DASHING_BWD instead of FWD, which I added before that :p so forget about

    Another edit:
    Would it matter if I make changes to the CharacterAnimationEvent directly from the Input script? Or is that a absolute no-go?:p
     
    Last edited: Nov 13, 2013
  21. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    I need to know exactly how you want dash to work in your game before I can help you in detail. There's a lot of ways you could do dash without changing the core code. I expect you would just need to create a custom input control.
     
  22. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Eh, it's hard to explain what I've changed already haha, But I got the following working: The dash animation, now the character needs to be shoved a bit backwards.

    I've done the change by calling the AnimationEvent directly with the following statement:
    Code (csharp):
    1. if(Input.GetAxis ("Horizontal") >= 0.1f  (Time.time - lastTapTime) < tapSpeed)
    2. {  
    3.     int dir = directionChecker.CurrentDirection;
    4.                
    5.     if(dir == 1)
    6.         characterAnimator.CharacterAnimationEvent(CharacterState.DASHING_BWD, CharacterState.WALKING);
    7.     else if(dir == -1)
    8.         characterAnimator.CharacterAnimationEvent(CharacterState.DASHING_FWD, CharacterState.WALKING);
    9. }
    It's not finished yet though, the directionCheck is because I've implemented a rotation to mouse position, which also needs to change the scale of the character, but only if a weapon is equipped etc. So thats why I needed to check direction.
     
  23. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Alright well just remember you can control the character by adjusting their Velocity.
     
  24. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    OK Thanks JohnnyA :) You've been a great help :) If you're interested in what I've done so far just send me a personal message (if it's possible on this Forum). I'd like to show the game I've been programming to an expert :)
     
  25. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    201
    Keep up the good work :) Sounds like a good solution.
    Btw. in Unitys new 4.3 sample (the one shown in the video that you modded) they control their characters using forces only. What's your take on this? Performance when doing it the physics way vs your way with raycasts etc? I always felt that doing it with physics was a bit like cheating and I imagine you wouldn't get the same "feel". Would you agree?
     
  26. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Unitys sample feels horrible, which is fine for a quick 2D sample, but not really acceptable for most games. The reason I created the controller in the first place is that in previous attempts I was constantly battling physics. Platform games, particularly 2D ones, don't tend to follow the laws of physics. That doesn't mean you *can't* do it with physics, its just you end up writing so many edge cases, you might as well just start from scratch :)

    Performance of the core control shouldn't be a problem in either solution. I stopped trying to optimise my raycasts many iterations ago, because even on mobile it just wasn't causing a noticeable performance hit.
     
  27. XxPleYxX

    XxPleYxX

    Joined:
    Jul 26, 2011
    Posts:
    26
    You can release in two separated packages like NGUI does, one package for 2D and the other one for 3D.
     
  28. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,166
    Hey Johnny,

    any news on the ropebridge :) ?
     
  29. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Hah was just thinking about that, got a bit sidetracked with the 2D updates to Unity this morning. Will have a look now.

    BTW: Is your rope bridge a single rope a lot like a tightrope? Or is it more a "log bridge" with multiple sections tied together and interacting with each other?

    - John A
     
  30. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,166
    Well, it looks like this:



    You can walk on each of these circles, so it's more a log bridge, I'd say.
     
  31. Dan_Tsukasa

    Dan_Tsukasa

    Joined:
    Jan 26, 2013
    Posts:
    156
    Oh this is interesting, you could quite easily replace those log graphics with planks (or do they only work if you're using capsule colliders (Only shape I can think of) and you can mae a variety of bridges from there.

    Have you simply set these up using hinge joints everywhere?

    I'm curious if your solution (when its solved) will be somewhat applicable to my idea of a swinging platform hanging on a chain (and you can push/pull this platform).
     
  32. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,166
    For now I just used BoxColliders since Capsule Colliders don't give me nice results with 2DPC and tried to mess with Platfrom-Scripts.
    No hinge-joints involved. Just Sprites with BoxColliders (for now).
     
  33. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    I started with physics but couldn't get it anywhere near good (using spring joints everything was fine except the bridge was too saggy no matter what settings I tried). Here's a custom job which still needs some tweaking mainly to be more "springy":

    http://dl.dropboxusercontent.com/u/60620789/RopeBridgeTest/RopeBridgeTest.html

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RopeBridgePlatform : Platform {
    6.     public RopeBridgePlatform myRight;
    7.     public RopeBridgePlatform myLeft;
    8.     public float offset = 0.1f;
    9.     public float speed = 10;
    10.     public Vector3 leeway;
    11.     public int feetRequired = 2;
    12.  
    13.     public int FeetCount {
    14.         get; private set;
    15.     }
    16.  
    17.     private Vector3 endPosition;
    18.     private Vector3 targetPosition;
    19.     private Vector3 startPosition;
    20.     private RaycastCharacterController myCharacter;
    21.  
    22.     override protected void DoStart() {
    23.         startPosition = myTransform.position;
    24.         targetPosition = myTransform.position;
    25.         endPosition = myTransform.position - new Vector3(0, offset, 0);
    26.     }
    27.  
    28.     void Update() {
    29.         FeetCount = 0;
    30.         CheckWithinRange();
    31.         myTransform.position = Vector3.Lerp(myTransform.position, targetPosition, RaycastCharacterController.FrameTime * speed);
    32.         float deltaY = (myLeft == null ? myTransform.position.y : myLeft.transform.position.y) - (myRight == null ? myTransform.position.y : myRight.transform.position.y);
    33.         float deltaX = (myLeft == null ? myTransform.position.x : myLeft.transform.position.x) - (myRight == null ? myTransform.position.x : myRight.transform.position.x);
    34.         myTransform.localRotation = Quaternion.Euler(0, 0, Mathf.Atan2(deltaY, deltaX) * Mathf.Rad2Deg);
    35.     }
    36.  
    37.     override public void DoAction(RaycastCollider collider, RaycastCharacterController character) {
    38.         // If we are standing on this
    39.         if (collider.direction == RC_Direction.DOWN) {
    40.             FeetCount++;
    41.             myCharacter = character;
    42.         }
    43.     }
    44.  
    45.     void LateUpdate() {
    46.         if (FeetCount >= feetRequired || myCharacter != null) {
    47.             targetPosition = endPosition;
    48.         } else {
    49.             targetPosition = new Vector3 (startPosition.x, (startPosition + (myLeft == null ? startPosition : myLeft.transform.position) +  (myRight == null ? startPosition : myRight.transform.position)).y / 3.0f, startPosition.z);
    50.  
    51.         }
    52.     }
    53.  
    54.     private void CheckWithinRange() {
    55.         if (myCharacter != null) {
    56.             if (Mathf.Abs (myCharacter.transform.position.x - myTransform.position.x) > leeway. x||
    57.                 Mathf.Abs (myCharacter.transform.position.y - myTransform.position.y) > leeway.y)
    58.                 myCharacter = null;
    59.         }
    60.  
    61.     }
    62. }
    63.  
    64.  

    Create a bunch of colliders and associate them with their neighbours. Position them so they are curved via their transform. For the two ends use an offset smaller than the others (say 0.05), settings on the others should be the same. Tweak the leeway so you don't get any "bobbling", for the sample I used (0.75, 1.25, 0), but it should be just a touch larger as it still can "bobble" up and down.

    Its after midnight, so I can't do any more tonight, but I'll try to add some springyness tomorrow.

    EDIT: Forgot you have to set the execution order so that this script guarantees to execute LateUpdate after the raycast character controller.
     
    Last edited: Nov 13, 2013
  34. Dan_Tsukasa

    Dan_Tsukasa

    Joined:
    Jan 26, 2013
    Posts:
    156
    For hingejoints you'd be good to use limits (but not motors), set the limit to something really low (I think, I'm not infront of unity right now), and you can get a rather nice look going on, I did a similar thing trying to create a rope between 2 objects along the X axis, it sagged in the middle but not in an unnatural way.
     
  35. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Hmm will give it a shot, I didn't think a hinge would be springy enough.
     
  36. fryiee

    fryiee

    Joined:
    Jan 28, 2013
    Posts:
    18
  37. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Hello JohnnyA, maybe this is a stupid question, but how can I move the character by altering it's velocity, I don't know exactly how it should work. I tried a few things.. butnone of them seem to work. I just change velocity with the following code:
    Code (csharp):
    1. character.Velocity = new Vector2(dashLength, character.Velocity.y);
    I call this code in a Update, which only activates if the bool (dashIsForward) returns true.

    I checked the bool and I know for sure because the code afterwards is also being called:
    Code (csharp):
    1. if(playerAnimation.CurrentFrame(true) == 5)
    2.             {
    3.                 if (!Input.GetButton ("Left")  !Input.GetButton ("Right"))
    4.                     characterAnimator.CharacterAnimationEvent(CharacterState.IDLE, CharacterState.DASHING_FWD);
    5.                 else if(Input.GetButton("Sprint")  Input.GetButton ("Left") || Input.GetButton("Sprint")  Input.GetButton ("Right"))
    6.                     characterAnimator.CharacterAnimationEvent(CharacterState.RUNNING, CharacterState.NONE);
    7.                 else if(Input.GetButton ("Left") || Input.GetButton ("Right"))
    8.                     characterAnimator.CharacterAnimationEvent(CharacterState.WALKING, CharacterState.NONE);
    9.  
    10.                 isDashing = false;
    11.             }

    Hmm could this be because I use DIGITAL movement?:eek:

    Edit:
    I am gonna do something you hopefully think is ok, I am gonna alter your controller a bit.

    I could send you the changes I'm going to make if you'd like :)
    Just let me know ;)

    Little update:

    After a hour and a half, I've implemented a dash, it works not fully how I want it, but I learned a lot about your controller, and for that I'd like to complement you about the good and helpful comments in your code. This truly is a good example for me, to learn programming! :D
     
    Last edited: Nov 15, 2013
  38. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Hey mate,

    you are right DIGITAL movement will prevent you applying physics type forces. You can get some of the way there with DITAL_WITH_SLIDE, but PHYSICS_LIKE is required for full force based interaction. An alternative is to switch modes on your character.

    For your dash solution editing the core code is probably the easiest way, I've got no problem with you doing that, you just have to be careful when you import in updates as you will lose your changes and will need to reapply them.

    - John A

    PS @fryiee I did take a look when you sent it to me, I just didn't have a nice/easy solution. Calculating the exit angle shouldn't be too hard (your character is already rotated to the right angle anyway). You probably need to do something special so that when in sliding mode you only rotate if all feet are colliding ( to stop rotation as you move off the edge). I think the next step would be to apply a velocity change when the character is in the sliding mode but only 1 feet collider is colliding. Thats extremely high-level and theres a lot of potential for complications, but maybe the use of the GroundedFeeCount will help you.

    I'm taking a look at Unity's new 2D physics in detail this weekend. Not sure it will be relevant but if anything pop-ups I will let you know.
     
  39. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    Hey Johnny,

    Has there been any chance to look at some of the suggestions from a few weeks back?

    I was also wondering if there had any possible changes to swimming (Regarding enter/exit water states) as well as that
    issue where it become difficult to jump out of water.

    (Generally the most Mario-example of it would be:

    > Swim up close to the surface of water
    > Jump upward while close enough to the surface of the water that you actually entirely jump out of the water for a reduced height jump. Although super unrealistic, it generally helps prevent people from just getting stuck in the water and unable to get out.

    Thanks!

    (Also that rope bridge example is rad, gonna snag that for future reference)
     
    Last edited: Nov 16, 2013
  40. kraaijmakers

    kraaijmakers

    Joined:
    May 2, 2013
    Posts:
    14
    Just wanted to let you know I've fixed the dash, it's almost exactly how we want it :) Though it's only implemented in Digital, I could send you the code changes I've made if you're interested ! :)

    Bye! :D
     
  41. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856

    HI just added wall jump with tag (and alternate velocity), I can send you the update if you would like.
     
    Last edited: Nov 16, 2013
  42. Kafar

    Kafar

    Joined:
    Nov 29, 2012
    Posts:
    207
    Hi John,

    I just sent an email.
    Thanks
     
  43. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    Thanks Johnny, that'd be great if you could.
     
    Last edited: Nov 17, 2013
  44. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Sent via PM.

    For everyone else it will be in the next release (you can email me if you relaly need these features now :) ).
     
  45. sirius_002

    sirius_002

    Joined:
    Aug 16, 2012
    Posts:
    65
    Hoping for "Simple melee and ranged attack examples." next, what is a ninja without shuriken and sword attacks. Another ninja game? Yes, hehe!
     
  46. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Next is definitely Unity Built in 2D sprite system (hopefully this weekend).

    But attacks are the next feature so hopefully I'll get to them in the next 2-3 weeks.

    - John A
     
  47. sirius_002

    sirius_002

    Joined:
    Aug 16, 2012
    Posts:
    65
    Good on ya mate, I'll be looking forward to it.
     
  48. fryiee

    fryiee

    Joined:
    Jan 28, 2013
    Posts:
    18
    Hi John,

    Thanks. I'll keep on plugging away and see what I can come up with.

    Thanks
    Craig
     
  49. secret-arts

    secret-arts

    Joined:
    Oct 4, 2012
    Posts:
    9
    Hi John,

    Sorry it's taken me a while to reply (had the flu), but your solution was very clear and worked perfectly. Thanks very much, problem solved!
     
  50. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    4,856
    Awesome, by the way to you (and everyone) I'm very keen on seeing what you are working on.

    I'm thinking of putting together a showcase video for the kit and for your games: basically I'd be after a short video snippet (or static screenshots) of your game with website or social details that you want included.

    I'll post 'em on my youtube channel and try to get some small amount of press for them; at the very least being on the channel should get us a few hundred hits and hopefully a few thousand.

    Let me know if interested.

    Regards,

    John A
     
unityunity