Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

[Released] Kinematic Character Controller

Discussion in 'Assets and Asset Store' started by PhilSA, Sep 29, 2017.

  1. rtargosz

    rtargosz

    Joined:
    Aug 22, 2012
    Posts:
    17
    I have now switched to event driven input handling with no input update loop and it is amazingly fast:
     
  2. billnguyenme

    billnguyenme

    Joined:
    Dec 5, 2020
    Posts:
    92
    Would you mind sharing the commands/codes used to implement into the original scripts? This is mainly for the active commands such as Jumping.
     
  3. rtargosz

    rtargosz

    Joined:
    Aug 22, 2012
    Posts:
    17
    I've dropped my 3 modified files for the input, camera and controller into a public Github repo here: https://github.com/rtargosz/BentVectorShared. This is still very much a WIP, but gives you some idea how to modify the example code from Philippe to use the new Input System package with KCC driven by events. My player is restricted to movement in the Z/Y access: left-right movement with A/D keys and jump with W, crouch with S. Player can interact with SPACE and fire with E/Left Control. Camera is controlled by a Cinemachine to facilitate cut-scenes, so there's no camera controller code.
     
  4. billnguyenme

    billnguyenme

    Joined:
    Dec 5, 2020
    Posts:
    92
    Thanks for the github link! I managed to switch over to the new input system after some trial and error using a different way. I think you're way is better than mine since it doesn't have to check for the jump trigger every frame so I'll use it as a reference.

    Example of what I did:
    playerInputActions.Movement.Movement.performed += x => input_Movement = x.ReadValue<Vector2>();
    playerInputActions.Movement.View.performed += x => input_View = x.ReadValue<Vector2>();


    My next goal is to figure out how to incorporate ledge hanging into this controller.
     
  5. URnewby

    URnewby

    Joined:
    Oct 2, 2018
    Posts:
    1
    I really like this controller but am having trouble with a Pause Menu. The Pause Menu works fine until I add your character controller. The buttons on the menu panel won't highlight and cannot be clicked.
    I have tried setting Time.timeScale=0f; Still doesn't work.
    Disabled all scripts. Still no worky.
    Disabled Audiolistener. Still no luck.
    Rick
     
  6. neo-mashiro

    neo-mashiro

    Joined:
    Aug 7, 2020
    Posts:
    16
    Does anyone know how to robustly detect if the character leaves off the ground? I need to know when the character gets into the air in order to play an airborne animation.

    Code (CSharp):
    1. bool inTheAir = !motor.GroundingStatus.IsStableOnGround;
    2. bool inTheAir = !motor.GroundingStatus.IsStableOnGround && motor.LastGroundingStatus.IsStableOnGround;
    For simple scenarios such as jumping or falling off the platform, the code above gets the job done. However, I found that this will also be triggered when the character tries to move up a steep slope that it cannot step onto.
     
  7. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    559
    Hi,

    Is there a way to prevent the character 'tightrope walking' along a collision edge, like this case with an almost-vertical plane? It seems possible to jump and land on these edges regardless of whether they're in a stable layer or not.

    tightrope.png

    I'm looking for a way to create colliders for scenery objects that the player will always slide off.

    (Also, I seem able to jump onto 'non-stable' slopes but the character doesn't slide down them - Is that normal? or a setup issue or bug?)
     
  8. billnguyenme

    billnguyenme

    Joined:
    Dec 5, 2020
    Posts:
    92
    Has anyone managed to add in a lock on system? There isn't any documentation for it and I can't seem to get it to work. I'm simply trying to have the player's rotation orient towards the target.
     
  9. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    30
    Anyway to skip the simulation for couple of frames? I have system which freezes animation upon hit, and Id like the motor to do that too.
     
  10. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Was this ever taken any further? I'm wondering if this could be used to simulate a box collider (Quake-style):
     
  11. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    PS: why has PhilSA not posted here since Apr 24, 2020?
     
  12. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    278
    He doesn't reply here much. I always had better luck when contacting him by e-mail.
     
  13. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    97
    Whats the correct way to rest your character?
    For example when your player dies and you reset his position.
    It seems I have to (at least) also reset the velocity?
    Anything else?
     
  14. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    84
  15. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    27,851
    There should be no performance difference. Do you mean fast to set up?
     
  16. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    868
    @PhilSA
    When the KinematicCharacterMotor is part of a prefab, it's impossible to remove (without modifying the source code) as the 'Remove Component' menu action, which seems to be custom, doesn't set the internals dirty, so the save button of the prefab mode doesn't become active.
    The solution is to replace the normal destroy calls with the undo equivalent
     
    Last edited: Feb 20, 2021
  17. simpa975

    simpa975

    Joined:
    Feb 15, 2019
    Posts:
    5
    Try to use "!motor.GroundingStatus.FoundAnyGround" instead. When the motor detects an unstable surface "IsStableOnGround" will return false, a steep slope counts as an unstable surface.
     
    neo-mashiro likes this.
  18. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    97
  19. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    84
    Whoops sorry I altered it over time, what I'm using now is:

    Code (CSharp):
    1. motor.enabled = false;
    2. motor.BaseVelocity = Vector3.zero;
    3. motor.AttachedRigidbodyVelocity = Vector3.zero;
    4. motor.SetCapsuleCollisionsActivation(false);
    And then to reenable it:
    Code (CSharp):
    1. motor.SetCapsuleCollisionsActivation(true);
    2. motor.enabled = true;
    3. motor.BaseVelocity = Vector3.zero;
    4. motor.AttachedRigidbodyVelocity = Vector3.zero;
    (You need to make AttachedRigidbodyVelocity public)
    Not sure if the order of these lines matters, but for some reason I wrote them like this lol
     
    Last edited: Mar 1, 2021
    BenniKo likes this.
  20. marccrobbins

    marccrobbins

    Joined:
    Nov 10, 2014
    Posts:
    2
    Have you ever tried to make an XR CharacterController with this? I recently attempted it with this package and the only issue I'm running into is the collision detection following my head(x, z).
     
  21. FcsVorfeed

    FcsVorfeed

    Joined:
    Aug 15, 2014
    Posts:
    38
    Still cannot access motor.AttachedRigidbodyVelocity
     
  22. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    84
    Just make it public, keep in mind if you update the asset you'll have to set it to public again
     
  23. billnguyenme

    billnguyenme

    Joined:
    Dec 5, 2020
    Posts:
    92
    I'm curious about the purpose of this code, does it actually translate the velocity in the CharacterController over to rigidbody? If so, this is exactly what I'm looking for in my projectile prediction script which is based on Rigidbody velocity/
     
  24. simpa975

    simpa975

    Joined:
    Feb 15, 2019
    Posts:
    5
    I know this is a very late reply, but if you're still having this issue I found a solution.

    Jitter on objects following Cinemachine camera - Unity Forum

    All I did was to put my "rotate character mesh script" underneath the CinemachineBrain script in the Script Execution Order.
     
  25. simpa975

    simpa975

    Joined:
    Feb 15, 2019
    Posts:
    5
    I saw one of your older posts about Ledge detection, and I'm wondering if you got anywhere with that solution? The one where you used a downward raycast in front of the character. I'm also looking to simulate a box collider at the bottom, I've seen it implemented on other CC solutions and I've tried to use there approach on this one, without any success so far.
     
  26. ChainsawFilms

    ChainsawFilms

    Joined:
    May 28, 2016
    Posts:
    18
    I'm trying to set this up with SteamVR. I've a main gameobject, and two children; one the Character Controller, one the Camera Rig.

    How do I translate input from the Character Controller to the parent, effectively keeping the Character Controller centred on the Camera Rig headset?

    Thank you
     
  27. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Yeah, I'm going to rewrite it soon, I'll post the final code eventually when it works. You're right, it could be used to create a box collider-type simulation for edges, but I'm not entirely sure how to override the built-in system with this.
     
  28. simpa975

    simpa975

    Joined:
    Feb 15, 2019
    Posts:
    5
    I actually sent an email to Philippe a couple of weeks ago, and regarding overriding the built-in system he wrote this; " In the ICharacterController, there is a "ProcessHitStabilityReport()" callback, which is called right after every time the CC evaluates the stability of a hit. In this callback, you can modify the "hitStabilityReport.IsStable" as you please to override whatever the default stability detection detected. The value you assign to "hitStabilityReport.IsStable" is what will be used".

    So if you override the "hitStabilityReport.IsStable" with the value you get from your own ledge detection system, it should work.
     
  29. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Haha, I actually emailed him also a couple of weeks ago and he said the same to me! I need to figure this out first, I assume this would remove the ability to slope slide at all?
     
  30. mathieu_mostlywizards

    mathieu_mostlywizards

    Joined:
    Dec 4, 2020
    Posts:
    5
    Hello everyone, I'm interrested by buying this package for prototyping.
    But I can't find an answer to my only question : how customizable it is?
    Let's say I just want to implement a dash, can I just write a new script and connect it?
    Would I be able to remove gravity or other momentum whitout touching the code package?

    Thanks :)
     
    Gooren likes this.
  31. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    278
    Highly customizable, with a solid guide docs, clean code, huge forum thread full of cool information PLUS a Discord server.
     
    mathieu_mostlywizards likes this.
  32. mathieu_mostlywizards

    mathieu_mostlywizards

    Joined:
    Dec 4, 2020
    Posts:
    5
    Well if I can add functionnalities modifying velocity or momentum outside the package, I'll go with it then ^^
    Thanks
     
  33. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    278
    Just be aware that it's not always obvious when to modify what at what point in time. Hence the package docs and this thread.
    Use the search feature, it is very much possible you will find someone dealing with the same thing in the past.
    Also in case you have any difficult unanswered questions/bug reports, write an email to the author. He rarely responds in this thread. And the Discord server I mentioned is "unofficial". So just write him an email. He will respond.
     
  34. Olog555

    Olog555

    Joined:
    Aug 20, 2017
    Posts:
    3
    I am experiencing some problems trying to connect multiplayer.
    1) Discrepancy of positions between server and client.
    I can assume that this is somehow connected with the processing of the input data, because if you use the buttons alternately with an interval, then everything works, but if I start walking WASD or ADAD then the position gets lost.
    2) I don't fully understand how to do the simulation in one frame to use it in matching and reusing input data.
     
  35. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Can crushing be detected with KCC? On doom, you can get crushed between ceilings and floors that descend on you, could this be done somehow with KCC or would it just always push you out of the way?
     
  36. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    108
    @Zebbi It would probably be simplest to do this with the physics system. Using the Collision.OnColliderEnter callback, detect the direction and velocity of the collider that hit your character and decide if it crushed your character. If you want to detect a crushing force from above, just get the dot product between your character's up vector and the vector from your character's root transform to the collision's hit point and check it against a threshold that you consider to be "from above". You could probably use the other rigidbody's mass to determine if it should be able to crush your character, or you could put crushing-capable objects on a certain layer or give them a tag or store data on one of their components to denote that they can crush and kill characters.
     
    Last edited: Mar 18, 2021
  37. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thanks! If it's solid, and therefore is an obstruction if you just up into it or hit it at the side when it's not close enough to crush, how would it work if you need it to crush into you? Surely it would break the collisions on KCC?
     
  38. meicexl

    meicexl

    Joined:
    Apr 9, 2020
    Posts:
    4
    You could just add a collider to the bottom of a moving platform and make it kill the player instantly.
     
  39. deiterate

    deiterate

    Joined:
    Jul 10, 2015
    Posts:
    9
    Have a look at the ExampleCharacterController->AfterCharacterUpdate() there is an example of disallowing standing up from a crouch when the player would not fit upright at its current location. A similar detection system could be used for detecting being squished. You could setup a new character state for being squished to animate it and handle it however you want.
     
  40. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    As I said, if you wanted to jump up and touch it, it would kill you. If it was a side crush, you wouldn't be able to touch it, etc.

    Thanks, that's interesting. Does that do a check to see if there's room to resize the capsule, and if not, it doesn't? I wonder how crushing could work into this, since technically you'd need the collider to actually be breached by the wall to trigger it.
     
  41. deiterate

    deiterate

    Joined:
    Jul 10, 2015
    Posts:
    9
    Hmmm. I see what you mean. It is setting the normal capsule size immediately before testing for overlap, but only if it was previously crouching and trying to uncrouch. It's highly efficient because it only does the extra overlaps in very specific circumstances.

    You could detect crushing in a similar manner by testing for overlap on a taller capsule while any ground is also detected below the character. Flagging as a potential crush action if an overlap is detected and setting the character capsule shorter than the standard player, so the next time through it can flag as an actual crush action if the test still detects an overlap on the normal character capsule size, otherwise return the capsule to normal size. But this seems inefficient, as it would be performing these extra overlap tests fairly often.
     
  42. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thanks, some great ideas there!

    Finally, is there a quick and dirty way of getting a first-person camera working out-of-the-box? I just need basic WASD input, mouse-look and the character controller, but the only example is third-person and looks like it requires heavy work to make it FPS-style.
     
  43. seoyeon222222

    seoyeon222222

    Joined:
    Nov 18, 2020
    Posts:
    29
    I saw the news about the new controller being developed on YouTube.
    I want to buy and test early access if there is a way to buy early access. q1) May I know the approximate launch schedule or roadmap? q2) Is it completely separate from the previous controller set? (If the previous assets is purchased, it is possible to upgrade or support it, or should it be purchased separately.)
     
  44. jeffreymlynch

    jeffreymlynch

    Joined:
    Jun 27, 2017
    Posts:
    5
    Hey, I was having a look at your code (thanks for sharing!) and just noticed that in your SetInputs() method you're calling
    mathf.Abs
    on the camera planar direction's square magnitude. This is unnecessary, as the square magnitude of a vector (which is basically adding together the squares of each vector component and returning the result) will always yield a positive number. In other words, the result of x^2 + y^2 + z ^2 will always be positive, even if the some or all of x, y, and z are negative.
     
  45. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    111
    Rarely my character will fall through the ground, it's hard to debug because it happens rarely, seems to happen when there is a force acted on the character.

    Any common place to look or a way to prevent falling through ground? The ground is a collider.
     
  46. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    278
    I have been troubleshooting similar issue with the author recently. The issue only happened on terrain colliders for me however. Is this the case for you as well?
    If so, make sure the terrain is not rotated. Terrains in Unity do not support rotation properly :D
     
  47. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    111
    my ground collider is a box collider. I think my character fall through ground when there is a impact force applied to them. but happens rarely, maybe something to do with direction of the impact.

    my kinematiccharactersystem is updated in fixedupdate

    I call the following to simulate impact.

    public void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime)
    {
    // Take into account additive velocity
    if (_internalVelocityAdd.sqrMagnitude > 0f)
    {
    Motor.ForceUnground(0.1f);
    currentVelocity += _internalVelocityAdd;
    _internalVelocityAdd = Vector3.zero;
    }
    }
     
    Last edited: Mar 24, 2021
  48. Olog555

    Olog555

    Joined:
    Aug 20, 2017
    Posts:
    3
    Sorry, I haven't figured it out for a long time. How do I re-simulate in one frame?
     
  49. seoyeon222222

    seoyeon222222

    Joined:
    Nov 18, 2020
    Posts:
    29
    Can i use the cinemachine exactly the same as the original camera controller?

    I tried using the third-person camera or orbital camera function of cinemachine.
    (using new inputsystem or tradional way)
    But it didn't really feel the same way as the previous camera

    The biggest difference I think is when the camera touches the floor.
    Original Camera controller follow the floor and and become closer to the character
    when the mouse is lowered. (Feels like camera have a colider)
    (regaredless of the zoom in/out)

    However, the cinemachine's orbital camera or third-person camera
    can only move the set trajectory

    perhaps i think this is a problem with my low proficiency of cinemachine,
    if you have implemented the same camera controller as before,
    could you show me how to setting cinemachine controller?
     
  50. julio_kolbapps

    julio_kolbapps

    Joined:
    Mar 2, 2021
    Posts:
    18
    Hello.
    I still don't use this component. I'm just reading the documentation to see if it meets my needs before I buy. My biggest doubt is in relation to PUN2. I read in the documentation some information on how to use this component with online games, but it is not clear there. Nothing detailed and no specific use case. In my case, my biggest interest is delay compensation.

    Objects that use this component create their own way of dealing with physics. So, how should I work with lag compensation? I also read something about it in the documentation. But it ends as soon as it starts. He is told that there are ways to deal with this, but he does not explain how. In the documentation he speaks only of games with Authoritative server. But I am using PUN2, Master Client.

    I just wanted to know how it all applies to the games made with him. Please, the author and or someone with this experience, could you let me know?
     
    KyleChallis likes this.
unityunity