Search Unity

[Released] Character Movement Fundamentals - A Rigidbody-based Character Controller

Discussion in 'Assets and Asset Store' started by JanOtt, Jul 1, 2019.

  1. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    That's the first time I've seen these specific errors - and I can't seem to reproduce them either on my machine (at least not yet).
    Which version of Unity are you currently using? Do the errors also occur if you import the package into a new, blank Unity project?

    Please feel free to contact me directly via the official support email (support@j-ott.com) and I'll get back to you as soon as possible!
     
  2. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
  3. path123456

    path123456

    Joined:
    Mar 20, 2021
    Posts:
    8
    Are there mobile controls and ability to crouch i played the demo I found it lacked those also ability to sprint.
    The gravity flip is very cool
     
    Last edited: Sep 26, 2021
  4. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Has anyone been able to get this to work with an Xbox controller and the new input system? I tried this tutorial and still nothing. Character movement works but no spinning the camera or jumping.
     
    indie3d likes this.
  5. Jigganis

    Jigganis

    Joined:
    Jun 19, 2019
    Posts:
    5
    Hey, I was just wondering, how extensively would I have to mod this engine for a skating game? Skateboarding OR inline. Trying to get a ballpark sense.
     
    indie3d likes this.
  6. Jigganis

    Jigganis

    Joined:
    Jun 19, 2019
    Posts:
    5
    My guess is that it would involve just making there be more inertia to turning and moving, ie you can't immediately turn 180 degrees or stop
     
  7. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Unfortunately, the CMF package doesn't feature any crouching/sprinting functionality at the moment, but you could implement this by writing a script that adjusts the controller's 'Movement Speed' based on whether a crouch/sprint key is pressed, for example.

    Regarding mobile input: If you haven't already, I'd recommend checking out chapter 7 (page 36) in the user manual, which covers the basics of how to write custom input scripts (which would probably be the easiest way to implement mobile input).

    If you already completed the tutorial, then you should have an 'InputMaster' script, which you can now use to get input from - all that's left to do now is to make sure that the 'CharacterInput'/'CameraInput' scripts on the controller access that input (either by creating a custom input script, as shown in chapter 7 of the user manual, or by modifying the included input scripts in the package).

    I hope this helps!
    I think it really depends on how 'realistic' you want the controller to behave/steer. If you just need to "fake" some acceleration/decceleration, then you could implement this by smoothing the input going into the controller (i.e. modifying the input script).
    However, if you want something more complex, then you could also try to create a custom controller script (see chapter 8, page 39 in the user manual for an introduction).

    The best approach really depends on the type of gameplay you're aiming for - can you think of an existing game that features the type of skateboarding movement you want in your project?
     
  8. Jigganis

    Jigganis

    Joined:
    Jun 19, 2019
    Posts:
    5
    Hey Mr. Ott, thanks for the reply. I'm really just going for Jet Set Radio (or, like, ps1 Tony Hawk) level physics. As simple as possible--like just a little more inertia to turning. Turning shouldn't immediately turn you to the left, it should take a second of holding the left key for that to happen
     
    Last edited: Sep 27, 2021
  9. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    I see - for that use-case, it might be a good idea to implement some kind of acceleration/deceleration system directly into the controller code.
    Essentially, you'd create a new variable to store the current movement velocity of the controller and increment that velocity toward the player's input over time.

    Feel free to contact me directly via the official support email (support@j-ott.com) if you need any help setting this up.
     
  10. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    Hello
    Thank you for this awesome asset. It really looks so promising.

    I have a biped robot with a couple of animations. I wanted to add a controller to this.

    Also what is the simplest approach for me here .
    I want to use a third person perspective view and move the robot around and using my animators?
    Cheers
    Hamid
     
  11. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    I'd recommend starting with one of the 'blank' controller prefabs included in the project and position your character mesh inside the red capsule (you can delete the capsule afterwards).
    Then, you'd set up your animations like you would with any other controller (create an animator controller, create animation variables, create states and transitions, then update your animation variables with a custom C# script every frame).
    If you're interested, chapter 10 in the user manual has some information on how to get the necessary controller state information (current movement speed, grounded state, etc.).

    I hope this helps!
     
  12. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    There is any solution out of the box to get this up and running for the Unity new input system? Without me writing bunch of scripts and all :D

    I like how you can just drag and drop the prefabs and just works but I would love to do this with the new input system.

    Thanks, great asset btw
     
  13. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!

    Unfortunately, there's currently no way to implement the new input system without writing any code. That said, the code you'd actually have to write is fairly straightforward - and if you're using an input action map (for example), I'd suspect that maybe ~4-5 lines of code would be all you need.

    Feel free to contact me directly (support@j-ott.com) if you run into any problems!
     
  14. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    Ok, I will see if i manage to do it.

    But just for curiosity, why not update the asset to work with the new input system also?
     
  15. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    There's a few reasons for this, but the main ones are:

    1) The new input system is not included in new Unity projects by default, so importing any package that requires it will cause a whole lot of compile errors and will make the project unuseable until the new input system has been imported as well (and a complete restart of the editor is required).
    My concern here would be that (especially) new Unity users would have a hard time figuring this out.

    2) The documentation for the new input system has improved considerably lately, but it's still very rough around the edges and too technical for beginners to make sense of, unfortunately.
    To add to the confusion, a lot of the tutorials (on Youtube, etc.) are outdated (since the new input system is still in active development and changing a lot).

    3) There's just too many ways to use the new input system (getting input directly from devices, actions, action maps, the PlayerInput component, the PlayerInputManager component, using action callbacks, polling input using actions, generating input action scripts ...).
    Which makes it very difficult to decide which approach to use and to include in the asset by default.

    So far, my solution to dealing with this has been to modularize the controller and separate the input from the actual controller code, which makes it fairly easy to implement any kind of input system.

    And as far as I can tell, if users want to create custom input action maps (which seems to be the most popular way to use the system), they'll have to write custom C# code anyway, to instance their custom input actions inside the various scripts and enable them, as well as get input from them.

    I hope that helps explains it a bit!
     
    lolhans likes this.
  16. Mysterius

    Mysterius

    Joined:
    Aug 24, 2015
    Posts:
    12
    Hello JanOtt,

    I'm having trouble using CMF with moving platforms animated with the help of DoTween.

    I currently have a very simple elevator going upwards thanks to
    Code (CSharp):
    1. DOMove(new Vector3(0, 500, 0), 10);
    I'm using the default FirstPersonWalker prefab for testing and when I hop on the elevator everything gets jerky. The character end up passing through the floor of the elevator.

    I tried the simplest method I know : parenting FirstPersonWalker to the elevator when the player is inside. Even that doesn't help, movement is still jerky.

    I took a look at your moving platforms script but I don't see how to apply that to my elevator moving thanks to tweens.

    Any hint? :)


    Edit : here's what it looks like.
     
    Last edited: Oct 14, 2021
  17. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    It's a bit hard to say what specifically causing the glitches in your video, but just to provide a bit of context:
    The moving platform system included in the CMF package uses kinematic rigidbodies that are moved in FixedUpdate(), in combination with triggerzones (=colliders with 'isTrigger' enabled) on top of the platforms.

    Whenever a controller steps on a platform, it also enters the triggerzone - and every time the platform is moved, it checks whether there are any controllers in its zone and moves them as well (using the built-in 'MovePosition()' function of the controller's rigidbody).

    You might want to try adding a similar triggerzone system into your elevator setup - or maybe even use the CMF platforms (and modify them if needed).

    Also, generally speaking: Parenting (non-kinematic) rigidbodies to moving objects almost always results in glitches and physics problems, so I wouldn't recommend that approach.
     
  18. Mysterius

    Mysterius

    Joined:
    Aug 24, 2015
    Posts:
    12
    Thank you for you answer :).
    After some testing it appears it was the SmoothPosition script that caused the issue. I have to use my own elevator script and the way I did it apparently didn't play well with SmoothPosition.

    I simply disabled the script when I enter the elvator and re-enable it when exiting.
     
  19. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Hello, I am going to use CMF for my game and like it very much. There is one problem I found that I would like to ask if it is easy to solve. Please see the screenshot: if the ceiling has a slope towards the ground and the character walks into this direction, the character gets pushed into the ground. Can this be fixed somehow?
     

    Attached Files:

  20. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!
    Unfortunately, his specific edge-case is not easily solveable - since the CMF controller is still a rigidbody, Unity's physics engine will automatically push it downwards (to resolve the collision), because the controller's collider doesn't extend all the way to the ground (by design).

    The easiest "solution" would be to avoid this kind of level geometry when designing your game's environment (if that's an option). Strategically placed invisible colliders/walls are also a good approach, of course.
    Alternatively, you could try to implement some kind of automatic crouching system: Maybe cast a ray upwards and force the controller into a crouching mode if the ray detects something.

    I hope this helps!
     
    jeroll3d likes this.
  21. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Unfortunately the players are able to built their own structures in our game, so we cannot avoid this kind of level geometry.
    But the idea with forcing the player to crouch sounds like a good idea. When crouched we are shrinking the capsule collider and it seems the smaller the collider, the less it gets pushed into the ground. So when crouched the problem is nearly not noticeable any more.
    Thanks for the help!
     
  22. jeroll3d

    jeroll3d

    Joined:
    Nov 6, 2010
    Posts:
    249
    I was watching your rare but extremely good videos on the internet (Youtube). It would be nice to have - if possible - more videos of you, something more educational indeed. I liked your teaching method. Congratulations my friend.

    :)
     
  23. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    I'm glad to hear you found them useful!
    Unfortunately, it takes me forever to create these videos, so I can't promise if/when there'll be more. But I'll see what I can do!
     
    jeroll3d likes this.
  24. Arokma

    Arokma

    Joined:
    Oct 14, 2015
    Posts:
    20
    Hello @JanOtt, we are approaching a year without an update, is the development still going ?
    Thanks
     
  25. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Almost all of my time spent on the CMF asset is currently going into support requests (emails), so there's not a lot of actual development happening at the moment.
    If there's going to be an update, it would mostly consist of bugfixes instead of new features though.
    Hope that helps!
     
    Arokma likes this.
  26. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Are we expected to use the old or new Input Systems?

    upload_2022-2-9_15-56-39.png
     
  27. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    The CMF asset by default comes setup with the old input system.
     
  28. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    General question: you say in the docs that you can switch between two controllers during gameplay. Would that be done by adding two different controller scripts (for instance AdvancedWalkerController.cs and ClickToMoveController.cs) to the player parent object and turning them on and off?
     
  29. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Yes, that would be the recommended method!
    You might need to update some secondary scripts ('TurnTowardControllerVelocity', or any animation related components) when the active controller script changes.
     
  30. unity_ftPO-4l1uh4FkQ

    unity_ftPO-4l1uh4FkQ

    Joined:
    May 1, 2021
    Posts:
    2
    Hello, I found this asset yesterday and I didn't buy it yet, but I am very interested. I want my character to climb and interact with physics and also be able to grab a rope and swing around (Tarzan style). Would this character controller be fit to develop that kind of movements?
     
  31. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hey!
    Just based on your description, a 100% rigidbody controlled solution would probably be a better choice for your project. The CMF controllers are based on rigidbody physics, but their movement is almost completely controlled by code (to achieve the same level of responsiveness as "normal" character controllers).

    As a result, you can't just add Unity's physics constraints (like a spring joint, to implement rope swinging) without modifications to the controller's source code (or custom C# scripts).

    I hope this helps!
     
  32. Zenuel

    Zenuel

    Joined:
    Nov 24, 2013
    Posts:
    1
    Hey! Very keen on this system and I was wondering if we should expect to see support for non-root motion ladders and rotating platforms any time soon?
    I can tackle them in my own extensions but obviously it would be a world of difference to save that time on a project by just dropping in your solution!
     
    HeyItsLollie likes this.
  33. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi! The problem with adding features like (for example) a ladder system is that there's a lot of different (and equally viable) ways to implement this: Depending on your project's gameplay, ladder design, level design, camera perspective, [...], not all of these approaches will be a good fit (or work well with other systems in your game).

    The same idea also applies to other things, like rotating platforms, doors, crouching/sprinting, and so on.

    That's why I'd rather keep the controller focused on just the basics of character movement and leave the implementation of specialized gameplay features the users.

    I hope that helps clear some things up!
     
  34. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    Maybe you could think about a module based approach. Keep the simple paradigm as is a valid argument but offer additional modules that can be turned on and off depending of the user needs.

    this is because you are asking beginners to code complex behaviors. A beginner want this asset because it doesn't know how to do all this movement and the rest. If he knew how to code going up ladders and platform he didn't need this asset. Or if he did need is just to cut on development time. But that is an advanced dev. Potential client yes but probably one that can create specialized setups anyway.
     
  35. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Unfortunately, a module-based approach wouldn't solve the underlying problem here either: There's just a lot of different approaches to implementing these advanced movement systems.

    Let's look at ladders again, just as an example. Here are just a few open questions that would need to be answered first:
    • How does the controller get on the ladder, detection-wise? Via trigger zones? Physics collision callback?
    • Does the controller get one the ladder automatically? Or does the player need to press a button? Can the controller access ladders in mid-air (while jumping)? Or only at the start/end of the ladder?
    • How does the controller get off a ladder? Can the controller be pushed off the ladder (from enemies, environmental hazards)? Can the player jump off the ladder at any point?
    • How does ladder movement work (in a physics sense)? Do we disable the controller and move the controller's transform directly? If so, how do we control where the movement is going (on what axes)? Via splines/curves? Or do we just disable the controller's gravity and convert forward/backward movement to up/down movement?
    • Is ladder-climbing tied to animations? Is there a 'get on/off' ladder animation? How do we sync the climbing animation to the distance of the ladder's individual steps?
    • Does ladder-climbing affect the camera in any way?
    • How does ladder-climbing interact with all the other systems in the package? What happens if the controller is climbing on a ladder, then enters one of the gravity-switching trigger zones? On that topic, can a controller access a ladder, if it is currently walking on a wall or ceiling? How do ladders and moving platforms interact?
    • [...]
    Depending on your game, you'd need to make different decisions here (include some features, while excluding others). You wouldn't use the same ladder system for an FPS like 'Half Life' and a third-person game like 'Dark Souls', for instance.

    If this asset was more focused ("Dark Souls Movement Fundamentals", just as an example), all of this would be a lot easier, and there would most likely be some kind of lader system included.

    I hope this explains it a bit!
     
  36. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    yeap it does :D
     
  37. JCardec

    JCardec

    Joined:
    Oct 26, 2015
    Posts:
    10
    HI!! I've been using your character movement fundamentals for over a year now and I must say it's a lifesaver!!! to be honest it's the only reason I still use the unity game engine (or at the very least not still figuring out how to make my own movement code) and successfully added multiples jump and dashing functionality thanks to your documentation!! however I do have one issue with adding lock on functionality using the built in RotateTowardPosition method. I keep getting null reference exception whilst using a very common looping statement to look at closest enemy. I'm asking here because on all the youtube vids and google searches it seems to work just fine for them and I'm at a lost for what to do. I can't help but feel that I must be missing something.
    Code (CSharp):
    1.  Transform GetClosestEnemy()
    2.     {
    3.         GameObject closest = null;
    4.         float distance = Mathf.Infinity;
    5.         Vector3 position = transform.position;
    6.         foreach (GameObject target in enemys)
    7.         {
    8.             Vector3 diff = target.transform.position - position;
    9.             float curDistance = diff.sqrMagnitude;
    10.             if (curDistance < distance)
    11.             {
    12.                 closest = target;
    13.                 distance = curDistance;
    14.             }
    15.         }
    16.         return closest.transform;
    17.     }
    Also, on a side note they should totally add your movement script as default with unity in a similar way they did with text mesh pro it's just that good!!! But here's the code in question, if you can help me that would be huge help!!!
     
  38. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Glad to hear you like the asset! Regarding your code:

    I could imagine that the 'enemys' list you're iterating over in your function might be causing the problem. Maybe there's 'null' entries inside, for example. Printing out its content would probably be a good idea to debug the problem.
     
  39. JCardec

    JCardec

    Joined:
    Oct 26, 2015
    Posts:
    10
    got it working thank you!!! turns out the camera was null this whole time and hadn't the slightest idea, had to make it public and drag and drop to get it to work. holy cow would've never guessed it because the controller was working fine and no errors was being thrown till I try calling the RotateTowards method. I forgot you the camera set up to work with and without the camera being assign in the CalulateMovementDirection method. kudos my good sir!!
     

    Attached Files:

  40. oBlissing

    oBlissing

    Joined:
    Oct 2, 2017
    Posts:
    12
    How do I rotate the camera instantly to a direction?

    Setting the transform.rotation on the CameraController doesn't work as the values are cached.
    Using
    cameraController.RotateTowardDirection(myTransform.forward, n)
    gives inconsistent results (tried using different values for the lookspeed).

    Basically I want to just set it instantly like
    cameraController.transform.rotation = myTransform.rotation;
     
    Last edited: May 7, 2022
  41. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    The 'RotateTowardDirection()' function automatically checks for overshooting, so if you want the camera to look at a target instantly, you could just use very high numbers (or Math.Infinity), like this:

    Code (CSharp):
    1. cameraController.RotateTowardDirection(yourDirection, Mathf.Infinity);
     
  42. oBlissing

    oBlissing

    Joined:
    Oct 2, 2017
    Posts:
    12
    Thank you! That worked. When trying "high values" before I went with
    10000
    and that wasn't high enough. Infinity to the rescue :)
     
  43. JungaBoon

    JungaBoon

    Joined:
    Aug 4, 2020
    Posts:
    18
    Hi,

    Is there a way to disable gravity temporarily? I want to create a wall-running ability with this controller. But when I set the gravity to 0f when he a wallrun trigger, the character begins to float (if jump was pressed when entering the trigger).
     
  44. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,

    Did you also contact me via email, by any chance? I just received a very similiar email, so if possible, it'd make more sense to continue there.

    Best wishes,
    Jan
     
  45. GesserX

    GesserX

    Joined:
    Dec 21, 2016
    Posts:
    8
    Hi! Thank you for the great Movement System. I have a stupid question maybe:
    I want the rotation of the character model be aligned with slopes normals. How to get the actual rotation I understand, but I'm struggling to apply it. Because it's always get overwritten or something. Of course I need to apply it to the model only, not to the mover with a collider.
    Thank you!
     
  46. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,

    You'll probably want to apply the calculated rotation to the 'Model' gameobject in the controller's hierarchy and disable the 'SmoothRotation' component there, or else the rotation might get overwritten.

    By the way, the 'Mover' component actually stores the current ground normal (only if the controller is grounded), so you can use its 'GetGroundNormal()' function and calculate the correct rotation with that.

    Hope this helps!
     
  47. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Hi @JanOtt. I've been digging into this asset (and docs) for quite awhile now, learning a whole lot, and I get that the controller scripts are the "brain" and the mover is the "body". But from a broad perspective, why did you separate the code out into these two scripts instead of combining them into one?
     
  48. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,

    The scripts are separated so you can create custom controller scripts (or "brains") without having to dig into the 'Mover's code as well.
    Beyond that, having the scripts separated also makes debugging and modifying the code easier. That's why (for example), the input scripts are separated from the controller script, or why the actual ground detection code is contained in a separate 'Sensor' class.
     
    Phenomonaut likes this.
  49. kamGames

    kamGames

    Joined:
    Apr 2, 2013
    Posts:
    33
    Hello I am new I was hoping to add some momentum to the character like they don't stop on a dime
     
  50. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,

    The easiest way to implement something like this would be to add some smoothing/interpolation to the input passed to the controller (so you'd need to add some code to the character input script attached at the controller's root).

    Alternatively, you could also modify the actual controller script to add an acceleration/decceleration system to its movement, but that'd be a lot more complex.