Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

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

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Awesome! Last question I have is regarding it's integration with visual scripting tools. I can code, but I enjoy working with tools such as Bolt or Flowcanvas which can usually only perform things inside Update, FixedUpdate, LateUpdate, Start, Awake and such, and a lot of character controllers I've tried from the asset store prefer movement code to be run from specific functions so they can be work in exactly the right order, but looking through the CMF manual, it seems that a movement script (or graph) can be written in FixedUpdate mostly?
     
  2. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Generally speaking, the package is meant to be modified and extended by writing C# code, but a few people have used it in combination with visual scripting systems (like Bolt), I believe.
    It really depends on your use-case, though: Feel free to contact me via email (support@j-ott.com) if you have any specific implementation questions and I'll try my best to answer them.
     
    Zebbi likes this.
  3. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    332
    Hi @JanOtt , I tried setting up the character controller in such a way that it stands firmly on a rotating, non-kinematic rigidbody based platform. The rotational movement of the platform is ignored though, no matter what I do.
    Is there something I'm missing? Or is that unsupported?

    This is my platform setup:
     

    Attached Files:

  4. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!
    The CMF controllers won't automatically inherit the current velocity/angular velocity of the rigidbody they are standing on, so you'd have to implement that functionality with a custom script, for example.

    I have done a few tests with rotating kinematic platforms in the past (which works fine so far), but I'm not sure how the controllers would react to a non-kinematic rotating platform: If the platform is non-kinematic, then the CMF controller is able to affect its rotation - which could lead to a feedback loop (the platform affects the controller, which affect the platform, which affects the controller....).

    That said, feel free to contact me directly via email (support@j-ott.com) with more details about your specific use-case and I'll try my best to help out!
     
  5. unity_oVf1I9PZ-6cRHw

    unity_oVf1I9PZ-6cRHw

    Joined:
    Jul 15, 2020
    Posts:
    1
    Hi there @JanOtt , I really love your asset! I struggled with controller programming for a few weeks and your creation made my life a lot easier. THANKS! Anyway I have one question, even though the code is described in detail, I'm a bit lost in all that (because I'm just not a programmer :)) ... I'd like to add the DASH and WALL JUMP function, but I don't know how to incorporate it into the code ... and overall how to write it. Can you please give me some hints or are you planning to add these functions in future versions?
    My project is side-platformer.

    Many Thanks
    Michal
     
  6. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!
    I wasn't really intending to add more advanced movement options (such as walljumps) to the controller, since there's just too many different ways to implement them (and not all approaches work equally well for all games).

    However, you could implement a walljump yourself by writing a simple script that keeps track of the collisions of the controller with the environment (using the 'OnCollisionEnter/Stay()' functions) and add/set the controller's momentum using the 'AddMomentum()'/SetMomentum()' functions, to push it away from the wall (based on the wall's surface normal).

    Implementing a dash is a bit more complicated, since there's lots of different types of dashes - feel free to contact me via email (support@j-ott.com) with a bit more information and I'll try to point you in the right direction!
     
  7. jeffreyjene

    jeffreyjene

    Joined:
    Jul 9, 2018
    Posts:
    14
    Hi Jan! I'm working with the AdvancedWalkerController in my game. At a point when the character gets hit I briefly enable root motion to allow the character to stagger backwards for a second, then I disable root motion again (I also disable the controller during this brief time). After reenabling, the camera is now misaligned just a bit. This will be compounded each time the character staggers. Can you think of a way to re-align the camera once the stagger is done or is this just not a good approach here? Thanks!

    Edit: You know what? I just realized you have a new feature in there, the AddMomentum! That worked perfect for a knockback. Anyone reading this, you need to get the transform of your model, not the controller then reverse it. I set the controller speed to 0 before doing this:

    Transform myTransform = GetComponentInChildren<Animator>().transform;
    controller.AddMomentum(-myTransform.forward * 20f);


    Then just restore the controller speed.

    Thanks for the new features Jan, I should have read about them first!
    :D
     
    Last edited: Dec 13, 2020
  8. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi Jeffrey,

    To answer your original question: By default, Unity's root motion is only applied to the gameobject that has the 'Animator' component attached - this means that your character model (which is down in the controller's hierarchy) is offset, but not the actual root of the controller.
    And of course, this offset stacks over time.

    If you still want to use root-motion, you could attach a script to the same gameobject as the 'Animator' and implement the 'OnAnimatorMove' function/callback - Unity will then stop applying the root-motion directly and instead pass the root-motion velocity to your script as a Vector3.
    You can then use this vector to move the root of the controller, for example (you'll still need to disable the controller script, of course).

    However, your solution with the momentum is perfectly fine as well - and has the added benefit of being easier to adjust (compared to an animation-based displacement).
     
  9. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Hi @JanOtt

    Could you tell me how you would go about scaling up and down the character at runtime. I'm trying to protoype the behaviour of eating things to get bigger.

    I've increased the Walker prefabs transform.localScale and transform.position.y to try and grow the Walker but it soon gets stuck in the floor geometry.
     
  10. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi @wxxhrt!

    Whenever you modify the controller's scale at runtime, make sure to call the 'Mover's 'RecalculateColliderDimensions()' function immediately afterwards - that way, the mover can recalibrate its internal raycast settings (which is necessary for the ground detection to work properly).

    I hope this helps!
     
  11. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Are there any plans for a flat bottomed capsule? ECM is the only controller asset I’ve seen that does this, it allows for characters to drop off edges naturally without lowering down.
     
  12. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    You can approximate this effect quite well with the CMF controller by selecting 'Raycast Array' as the detection mode of the 'Mover' - just increase the number of raycasts a bit (~9 should suffice) and the controller should stay on top of most edges, without lowering down.

    I believe that's how the ECM controller works as well, internally. If I recall correctly, they also use a simple capsule collider for the controller's body.
     
  13. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Does CMF support rotation platforms, being pushed by objects, weighing down bridges, etc? I've been comparing the three main rigidbody controllers (this, ECM and CCP... not KCC because it's kinematic) and noticed a lot of dynamic environment tricks in the CCP example (
    ), but I noticed an old post in this thread that mentioned CMF didn't support rotating platforms?
     
  14. JohnRamRu

    JohnRamRu

    Joined:
    Nov 16, 2019
    Posts:
    2
    Hi!

    I have trouble then I increase my jump speed to high (expl: 50). If I jump and some block is above me I stuck in the air (with saving velocity). After a few seconds - the same as I normally falling. How can I fix that?

    Thanks!
     
  15. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Currently, CMF does not come with rotating platform support out of the box - although it shouldn't be too hard to implement, depending on how you want the rotation to be applied (only to the character, or to the camera as well).
    I was planning to look into the moving platform system a bit, when the current Unit sale is over - maybe I can find a good way to add rotation support in there!

    By default, the CMF controllers will be pushed by other rigidbodies (if they are heavy enough or set to 'isKinematic').

    Weighing down other ridigbodies (such as bridges) can be "faked", by applying a force to whichever rigidbody the controller is currently standing on - this is fairly easy to implement.
    Whether this 'fake' weight is sufficient for your game depends on your specific use case - feel free to contact me with more information on that and I can go into more detail!

    I'd recommend adding a 'Ceiling Detector' component to the root of your controller. This (optional) component will use Unity's physics callbacks to detect collisions with colliders above the controller and should solve the issue you're describing.
     
  16. JohnRamRu

    JohnRamRu

    Joined:
    Nov 16, 2019
    Posts:
    2
    Thanks! It helped me a lot!

    Its so fun to use "tube" with high jump speed :D
     
  17. ridlr

    ridlr

    Joined:
    Mar 4, 2020
    Posts:
    25
    Hi JanOtt. This question isn't super specific to your package, but I thought you might be able to help me out. How do you move the camera when using the FPS controller? (assuming you're using a rigidbody system for the player) In the game I am currently working on, when moving the player's rigidbody and turning the camera at the same time, a lot of blurriness occurs. I am setting the position and rotation of the camera in Update() and adding forces to the rigidbody in FixedUpdate(). I have interpolation enabled on the rigidbody, but it doesn't seem to fix the issue.
     
  18. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    The camera is just a child object of the controller/rigidbody in all the CMF controller prefabs. As a result, it will always move with the controller.
    Did you check that the rigidbody interpolation is actually working in your setup? Try setting the game to slow motion (using Time.timeScale), to get a better look.
    Also, it's important to remember that rotating/moving a rigidbody directly (via the transform component) will break the interpolation - not sure if that's what's causing your issue, but it's a very common mistake.
     
  19. ridlr

    ridlr

    Joined:
    Mar 4, 2020
    Posts:
    25
    Ohhhhhhh, I was using transform.rotation to set the horizontal rotation of the player mesh (on the same object as the rigidbody). I made the mesh object a child and the camera a child of that and changed all previous transform.rotation's to playerMesh.rotation's. Now it works! Thank you so much!
     
  20. BraCa

    BraCa

    Joined:
    Mar 26, 2013
    Posts:
    1
    Hi,

    I'm playing with this, to see how it works, and so far it's good.

    But I would like to know, how can I make this work with surfaces that have a physics material applied to it, for example, I might have an area where the floor has a physics material with a higher bounciness, or with more friction
     
  21. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,

    The CMF controllers don't really collide with the ground, technically (they use raycasts for ground detection) - as a result, any physics material on the ground collider will be ignored.

    Generally speaking, the controllers are based on rigidbody physics, but they don't 100% act like rigidbodies. Each controller calculates and sets its velocity every frame, so any effect that physics materials might have will be overridden immediately.

    That said, you could approximate something like a high-friction surface by dynamically decreasing the controller's movement speed (as long as it stands on the surface), for example. Or apply an upwards momentum everytime the controller lands on a 'bouncy' surface.
     
  22. Fanttum

    Fanttum

    Joined:
    May 18, 2014
    Posts:
    40
    This would be great! I've having a tough time figuring out how to keep the character on a moving and rotating platform.
     
  23. Elyaradine

    Elyaradine

    Joined:
    Aug 13, 2015
    Posts:
    27
    I picked this up a while ago during a sale intending to play with it later. That day has arrived, and I've been really impressed with how this has been put together. I'm aiming to make something with very stylised motion, so I'm looking forward to experimenting and seeing how easy that ends up being.

    I did see a minor thing in your documentation, and thought I'd ask in case it becomes an issue. On p43 in the documentation, you multiply gravity by Time.deltaTime. I was wondering if this should really be Time.fixedDeltaTime, given that it's running in FixedUpdate. Otherwise I may be misunderstanding how the system works.
     
  24. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi, glad to hear you like the asset so far!

    To answer your question: Unity will automatically return the correct delta time, based on whether 'Time.deltaTime' is accessed in 'Update()' or 'FixedUpdate()' (at which point it will return 'Time.fixedDeltaTime').

    That said, writing 'Time.fixedDeltaTime' (even if it's technically not needed) is the more readable option, so thanks for bringing this up - I'll try to update the manual in the next update to prevent any potential ambiguity regarding this in the future.
     
  25. mindravelinteractive

    mindravelinteractive

    Joined:
    Jun 26, 2018
    Posts:
    15
    Hey @JanOtt , How do you completely disable the "jumping" in click and move example?
     
  26. p13t3rm

    p13t3rm

    Joined:
    Nov 21, 2013
    Posts:
    21
    Hey @JanOtt , been digging this package for my game, but just ran into a minor issue and was wondering if you could help?

    I have a script that sets the Time.timeScale value to 0 whenever a menu is open and back to 1 when it's closed.
    For some reason that seems to trip up the CameraController scripts rotation functionality and it'll prevent me from looking around after the menu has been closed.

    upload_2021-3-2_12-4-55.png

    Any idea why that may be happening?
     
  27. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    I'm sure JanOtt can you give you a more asset-specific solution here...but....IMHO you shouldn't set timescale to 0 in this case but try 0.001. 0 can cause issues (such as this) where a very small value will generally give the same desired outcome without the issues.
     
  28. p13t3rm

    p13t3rm

    Joined:
    Nov 21, 2013
    Posts:
    21
    Interesting, this script is from a Dialogue Manager menu script, but modifying the timeScale to
    0.00001f fixed it.

    Thanks for the tip @toddkc !

    Edit:
    After more testing, it appears that it only slightly fixed the problem but it springs up again after a while. -.-
     
    Last edited: Mar 2, 2021
    toddkc likes this.
  29. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    I'm not 100% sure what you mean by 'jumping' here - the 'ClickToMoveWalker' prefab doesn't really have any jumping functionality at all, just basic gravity calculation. Could you elaborate a bit?

    Hi! What you're describing is a actually a bug re-introduced in version 2.1 - I'm planning to include the fix in the next version though.
    In the meantime, feel free to contact me at 'support@j-ott.com' and I'll send you the instructions to fix it (it's a very straightforward fix)!
     
    p13t3rm and toddkc like this.
  30. wslater74

    wslater74

    Joined:
    Feb 11, 2021
    Posts:
    2
    Hey @JanOtt in comparison to every issue posted mine seems the most stupid but I honestly can't solve it.

    When using any gamepad I can't get any of the buttons to work for "Jump" .

    For clarification using Spacebar does work for jump and the gamepad works fine at with other inputs like the thumbsticks.

    Everything is correctly set up in the input manager I believe. Screenshot (2).png
     
    Last edited: Mar 24, 2021
  31. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!

    All of the input scripts included in the package use Unity's 'Input.GetKey(KeyCode)' function (instead of the axes set up in the input manager) for jump input - so your "Jump" input axis isn't actually being used.

    If you want to use your gamepad's buttons to control the jump, you could select 'joystick button 0' for the 'Jump Key' in the 'CharacterInput' script attached to your controller.
    Alternatively, you could also modify the 'CharacterInput' script as well, so it gets its jump input from the input manager axes instead.

    I hope this helps!
     
  32. wslater74

    wslater74

    Joined:
    Feb 11, 2021
    Posts:
    2

    Perfect, Thank you I truely appriciate your tollerence for helping idiots like myself.
     
  33. jammer42777

    jammer42777

    Joined:
    Apr 21, 2014
    Posts:
    116
    Hi,
    So I'm using the character controller to get my sidescroller together.
    I am wondering, how do I change the mesh in the sidescroller prefab?

    Thanks!
    --Joshua
     
  34. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!
    Basically, you'd start with the 'blank' variant of the sidescroller controller prefab and add your custom mesh to it (by parenting it to the hierarchy).
    Afterwards, you'd then set up the character's animations just like you would with any other character controller (calculating relevant attributes like current movement speed and passing them to the animator controller, for example).

    There's also a short guide/tutorial available on how to connect the controller's movement to the character's animations, if you're interested:
     
  35. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi everyone! Version 2.2 has just been approved by Unity and can be downloaded at the Asset Store now.

    NOTE:
    All controller air/slope movement has been overhauled in this version, so please make sure to create a safety backup of your project before updating, if you made any changes to the scripts in the package!

    MOVER:
    - Added 'SetColliderThickness()' function to 'Mover', which can be used to change the thickness/width of the controller at runtime.
    - Most variables related to collider size inside the 'Mover' script are now not public anymore (since they really shouldn't be changed directly at runtime).
    - Instead, changing the controller's size/shape at runtime should be done only via the 'SetColliderHeight()'/'SetColliderThickness()'/'SetStepHeightRatio()' functions, which will automatically update the ground detection system based on the new size/shape.

    CONTROLLERS:
    - All air movement calculations inside 'AdvancedWalkerController' have been completely overhauled and now properly use the controller's momentum.
    - The 'Air Control' setting has been replaced with 'Air Control Rate' (higher values result in a more direct movement while the controller is in the air).
    - The slope movement calculations have also been overhauled and are now more effective at preventing the controller from going up slopes (which are too steep, based on slope limit).
    - A few minor, general code improvements to the 'AdvancedWalkerController'.

    CAMERA SYSTEM:
    - Fixed a bug inside 'Camera Mouse Input' that resulted in a 'NaN' error when setting Time.timeScale to '0f' at runtime.
    - Fixed a bug inside 'Camera Distance Raycaster' that could result in incorrect layermask settings.

    MISC:
    - A few minor code improvements to the 'AnimationControl' script.

    As always, you can contact me via email (support@j-ott.com) if you have any questions about the package or the new update!
     
  36. kwanggan

    kwanggan

    Joined:
    Feb 26, 2021
    Posts:
    3
    Hi, I'm a newbie to unity. I have before buying question
    My requirement is needed to create a game that needs a movement like double jumping/swimming / climbing the wall /air dash / and fly with some rocket that also works with PUN2.
    Is this asset can reach my requirement ( I can coding as full stack web developer )
    Please suggest me.
     
  37. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi!
    By default, the asset comes with a few example controller prefabs, which cover the basics of walking/jumping movement.
    Something simple like double-jumping can be implemented with a few modifications to the the controller code, while other, more complex movement options (like swimming/climbing/flying/...) are probably easier to add by creating separate controller scripts for them and switching between them at run-time, whenever necessary.

    To get a better idea of the coding part of things, I'd maybe suggest checking out the online manual, especially chapter 8 (page 39), which goes over how to create custom controller scripts.

    Regarding PUN2: The asset doesn't come with any networking functionality, but I know that several users have successfully used the asset in networked multiplayer games.
    However, I myself don't have a lot of experience with networked multiplayer in general, so I can't really offer too much support beyond the basics here.

    I hope that helps!
     
  38. kwanggan

    kwanggan

    Joined:
    Feb 26, 2021
    Posts:
    3
    Thanks for your reply.

    I love how smooth of movement even it on webGL demo site. So I decided to go with it :D
     
  39. Diocr

    Diocr

    Joined:
    Sep 17, 2019
    Posts:
    1
    Hi! I'm just curious if this package has any ragdoll examples?
     
  40. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    No, the package doesn't feature any ragdoll functionality by default, so this is something you'd have to implement/add yourself by writing a few C# scripts.
     
  41. denizog1801

    denizog1801

    Joined:
    Apr 25, 2021
    Posts:
    1
    Hi, I can not get controller support to work. Even I add the scripts. It doesn't get any input. I need help with that
     
  42. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi! New Unity projects don't come with all controller input axes setup by default, so you may have to add and configure them first using Unity's built-in input manager.
    Please feel free to contact me directly via the official support email ('support@j-ott.com') if you need help with that!
     
    Last edited: May 3, 2021
  43. hudu233

    hudu233

    Joined:
    May 14, 2021
    Posts:
    1
    I have purchased your product, I need a spin jump like csgo, what kind of prefab should I use and how to adjust the parameters.
     
  44. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi! I'm not really sure what you mean by 'spin jump' here unfortunately - could you maybe contact me with more information (a video showing that type of jump in action, for example) at 'support@j-ott.com'?
     
  45. SomeHumbleOnion

    SomeHumbleOnion

    Joined:
    Jan 18, 2021
    Posts:
    28
    Hello!

    I was wonder how to properly disable the controller script via another script? Currently I'm trying to hand over the player movement from the 'Advanced Walker Controller' script to a custom wall climbing script I made once the player switches to a climbing state.

    I'm disabling the advanced walker script in my climbing script but when I do this the player starts to slide and move on it's own as if it was in a zero gravity area. I'm assuming this is because I'm disabling the gravity variable when I disable the controller script so I'm curious if there are other variables or scripts I need to reset or disable when disabling the advanced walker controller script. Thanks!
     
  46. SomeHumbleOnion

    SomeHumbleOnion

    Joined:
    Jan 18, 2021
    Posts:
    28
    Woops nevermind I forget to switch the rigidbody to kinematic when disabling the controller script!
     
  47. BIGBOSS1987

    BIGBOSS1987

    Joined:
    Feb 28, 2021
    Posts:
    1
    Hi, I thought a lot, who to turn to for advice on the character controller, namely how to implement so that my character cannot climb, on steep slopes? I was advised to ask you
     
  48. JanOtt

    JanOtt

    Joined:
    Apr 23, 2019
    Posts:
    183
    Hi,
    Generally speaking, you can change the 'Slope Limit' of the 'AdvancedWalkerController', which should result in the controller sliding down steep slopes (depending on the surface normal).
    Also, adjusting the 'SlideGravity' setting can be helpful to tweak the controller's movement on slopes as well.
    Please feel free to contact me directly via the official support email ('support@j-ott.com') with more information and I'll try to provide more specific solutions/advice!
     
  49. livium

    livium

    Joined:
    Aug 3, 2018
    Posts:
    27
    is it possible to use the asset in a multiplayer game (Photon)? what changes should I make?
     
  50. morristreet

    morristreet

    Joined:
    Jun 29, 2017
    Posts:
    3
    Just got this script as part of the latest Humble Bundle
    When importing it, I get 3 errors right out of the box, as it were.
    Assets\Character Movement Fundamentals\Source\Scripts\Controllers\ClickToMoveController.cs(263,23): error CS0115: 'ClickToMoveController.IsGrounded()': no suitable method found to override
    Assets\Character Movement Fundamentals\Source\Scripts\Controllers\ClickToMoveController.cs(268,26): error CS0115: 'ClickToMoveController.GetMovementVelocity()': no suitable method found to override
    Assets\Character Movement Fundamentals\Source\Scripts\Controllers\ClickToMoveController.cs(273,26): error CS0115: 'ClickToMoveController.GetVelocity()': no suitable method found to override

    So, as it stands, the entire package seems unusable - any ideas?