Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[Released] Kinematic Character Controller

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

  1. Mirath

    Mirath

    Joined:
    Aug 5, 2012
    Posts:
    11
    And an example of the collider drift when pausing the simulation:
     

    Attached Files:

  2. heartshapedbox

    heartshapedbox

    Joined:
    May 27, 2018
    Posts:
    27
    Is there a way to get simulated position?
    I mean, i want to get a next calculated position not my character actually move.
    I'll cast a ray at that position and decide my character move or not.
    how can i get next calculated position?
     
  3. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    You can check overlaps with motor.CharacterCollisionsOverlap at that position, it wont actually simulate you moving there from your current one but if youre just teleporting this works well.

    Also since the character is a capsule I recommend to do a spherecast with the players radius downwards from the raycasts hit point + up vector to get a working position

    If youre not teleporting and I misunderstood you, you can use Motor.TransientPosition during the UpdateVelocity() phase to get the next position. Read the UpdateVelocity() part of the water swimming example movement script, this should be what you want. The part where it does this:
    Code (CSharp):
    1. Vector3 waterSurfaceNormal = (resultingSwimmingReferancePosition - closestPointWaterSurface).normalized;
    2. smoothedVelocity = Vector3.ProjectOnPlane(smoothedVelocity, waterSurfaceNormal);
    You just use your raycast hit normal or something as the surfaceNormal, that should work
     
  4. heartshapedbox

    heartshapedbox

    Joined:
    May 27, 2018
    Posts:
    27
    Thanks for the reply!

    I don't teleport. i want my character move along the wall (hanging the ledge and move side to side).
    and at first, i tried to use TransientPosition in UpdateVelocity() as you suggested,
    but it does not help because it was not different from transform.position.
    I want to cast the ray from next position, but it does not calculated yet.. right?

    TransientPosition value changes after the UpdateVelocity called.
    so i use TransientPosition in AfterCharacterUpdate().
    In AfterCharacterUpdate(), i cast a ray from TransientPosition and get a hitpoint,
    use Motor.SetPosition() to that hitpoin.
    but i'm not sure this is the correct way...
     
    Last edited: Nov 5, 2022
  5. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    The swimming example should do what you want though as it modifies the characters velocity so it never goes beyond the hit point/normal. Also transientPosition is likely very similar to your current one, since frames (and therefore positions) are very close together if you have lots of them xD
     
  6. heartshapedbox

    heartshapedbox

    Joined:
    May 27, 2018
    Posts:
    27
    ah, okay. i'll check swimming example again.thank you!!
     
  7. heartshapedbox

    heartshapedbox

    Joined:
    May 27, 2018
    Posts:
    27
    I solved my problem with your suggestion!
    Those who want to check simulated position in UpdateVelocity, "resultingSwimmingReferancePosition" is what you want.

    and i have another question,,
    in your project, does the character interact with object (pull or push or throw..) by KCC?
    or use different logic?

    Thank you again!!
     
    Vincent13122 likes this.
  8. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    Awesome! I think if it collides with any physics objects it should push them away, with the rigidbody interaction settings in the kcc motor set to simulated dynamic
     
  9. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    785
    How can i ensure that when i place a Kinematic Character Controller on a certain position, using motor.SetPosition, they wont fall through the ground colliders?

    At the moment i place them on our generated navmesh, and that sometimes causes our characters to fall through. What settings would affect this?
     
  10. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    I am spawning the player on the navmesh too, but never had that problem so far. The navmesh is only an approximation of the environment, so maybe its related to how detailed your generated navmesh is or if youre using colliders instead of meshes to generate it.

    Either way, maybe you can do a raycast downwards from the spawn position on the navmesh + 1 on the y axis or something and if it hits something, use that as the spawn position. You could also generate a height mesh instead of a navmesh which will be more accurate to the mesh/collider
     
  11. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    785
    Thanks! Using the heightmap is a great suggestion.
     
    Vincent13122 likes this.
  12. socialtrens

    socialtrens

    Joined:
    Oct 23, 2017
    Posts:
    37
    Has someone tried putting Unity Ragdoll on top of PhysicMover? I did and it crashes Unity Editor. I need to restart afterwards and try again and it crashes again. Is PhysicMover incompatible with Unity Ragdoll? How to make it work?
     
  13. TAPE_EATER

    TAPE_EATER

    Joined:
    Nov 2, 2019
    Posts:
    13

    I'm trying to implement a ledge grab as well, I have two raycasts (forward, down) and two capsulecasts (up, forward) to detect when and where to hang (the hang position is the forward raycast.x and down.y).

    But when I set the position with "Motor.SetPosition", the character glitches for a frame and sometimes gets put at half the height of the object to hang on to. I'm guessing this is because it overlaps and I tried to work with the suggested motor.CharacterCollisionsOverlap but I'm not sure how to actually use it to set the character position.
    Would you mind elaborating?
     
  14. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    motor.CharacterCollisionsOverlap checks if a position can be safely occupied by the character, so only if it returns 0 (no overlaps found) you can move the character to the position you checked. So what you have to do ideally is have the position you check be where you will move the character and only check it to make sure it can go there and prevent the character from climbing if it cant.

    Though finding the ideal position I cant really help you with as it can be a pretty complex topic depending on what exactly you want to do
     
  15. TAPE_EATER

    TAPE_EATER

    Joined:
    Nov 2, 2019
    Posts:
    13
    So it's pretty much the final cast to see if the motor can move there.
    I put it in now, and it actually works allright with the given position of the casts (placing the character on top of the object to climb), i was setting the hang position offset wrong and it didn't work on all angles/positions. So i have to still figure out what the best way is to set the offset from the ledge.

    Thanks for clarifying
     
    Vincent13122 likes this.
  16. jmgek

    jmgek

    Joined:
    Dec 9, 2012
    Posts:
    72
    Does, anyone have a clean way to handle enemy targeting? Just need the controller to move a few feet to the target and rotate towards the target to handle some of the animation issues we're having with a hack and slash game.
     
  17. Mirath

    Mirath

    Joined:
    Aug 5, 2012
    Posts:
    11
    Hiya, did you ever figure out the cause of this? Currently dealing with the same and would like to figure out what to change to have it behave more consistently.
     
  18. rafaelcraftero_unity

    rafaelcraftero_unity

    Joined:
    Jun 30, 2020
    Posts:
    8
    Hi everyone, i've been struggling with the kcc because when im jumping and it hits the ceiling it immediately stop the momentum and goes down and i need it to doesn't happen, do you know how to do it?
     
  19. TAPE_EATER

    TAPE_EATER

    Joined:
    Nov 2, 2019
    Posts:
    13
    I have tried a couple of ways to add extra velocity for a launchpad based on the example scenes. But it seems that the velocity that's added is not consistent with the different states the character can be in. For instance when I jump on a launchpad it bounces the player up only a small fraction of the velocity the player gets entering the launchpad hitbox when grounded even though I am setting the current velocity to Vector3.zero before adding the extra velocity with Character.AddVelocity(Vector3.up * 50f).


    So not jumping and walking into the hitbox will add the velocity like expected and continues to work every consecutive landing on the hitbox, but landing in the hitbox from a jumping state (having pressed the jump button) will dampen the velocity to almost no bounce at all and will not be a consistent amount of movement each time.


    Does anyone have any pointers for getting consistent and stable velocity impulses like this?
     
  20. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    209
    Hi, I wanna know if you guys let a lot of enemies using Motor?
    The Motor of KCC is a robust system to handle movement, but look at it, multi thousands of lines of code. Is it good to use for multiple enemies rather than the main character?
    If you don't use it, can suggest any system for enemies' movement thanks!
     
  21. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    It runs surprisingly well actually, but it of course depends on how many enemies your game has. You can check how much processing time it takes in the profiler with deep profile enabled. I would also check if your enemies even need to have physics enabled, if you just have them move on the navmesh without physics, that will be the fastest.
     
  22. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    209
    That is be great option. thanks for pointing out. I'm currently inspecting Flock Box DOTs. It looks like it covers lots of use cases. Can suit my needs.
     
    Vincent13122 likes this.
  23. rafaelcraftero_unity

    rafaelcraftero_unity

    Joined:
    Jun 30, 2020
    Posts:
    8
    Could anyone help me?
     
  24. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    158
    I havent tried this but maybe there is some code that adds velocity downwards if a collision is detected above the player whilst hes not grounded and going up, just remove that code and it might work, but I am not sure (If there is, it should be in the motor script)
     
  25. chris73it

    chris73it

    Joined:
    Oct 15, 2013
    Posts:
    20
    Hi, I would like to join the Discord community, but I cannot find the link anywhere. If it exists, I would like to know what is the special shake to enter this exclusive club :) Thank you!
     
  26. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    209
    My character sometimes be launched at too much force, especially in contacts with lots of ragdoll colliders from other character, or sliding off a ledge.
    launching at neck-break speed looks fun but not easy on controlling gameplay.
    Do any one handle this?

    I tried limit air speed in UpdateVelocity method, but got no luck
    Code (CSharp):
    1.                                 //limit air move spd
    2.                                 var clampedMagnitude = math.clamp(currentVelocity.magnitude, 0, math.max(MaxAirMoveSpeed, JumpUpSpeed));
    3.                                 currentVelocity = currentVelocity.normalized * clampedMagnitude;
    4.                             }
    Obviously, this code tries limit speed to air spd and jump spd, it will not cover the case where character launched by attacks. But I'll handle that later, currently I want to limit speed when character launched unintended.
     
  27. raykingqd

    raykingqd

    Joined:
    Feb 11, 2020
    Posts:
    1
    unity2022 no longer supports setting Rigidbody.velocity and angularVelocity directly. how can I solve this problem?
    Code (CSharp):
    1. Setting angular velocity of a kinematic body is not supported.
    2. UnityEngine.Rigidbody:set_angularVelocity (UnityEngine.Vector3)
    3. KinematicCharacterController.PhysicsMover:VelocityUpdate (single) (at Assets/KinematicCharacterController/Core/PhysicsMover.cs:249)
    4. KinematicCharacterController.KinematicCharacterSystem:Simulate (single,System.Collections.Generic.List`1<KinematicCharacterController.KinematicCharacterMotor>,System.Collections.Generic.List`1<KinematicCharacterController.PhysicsMover>) (at Assets/KinematicCharacterController/Core/KinematicCharacterSystem.cs:192)
    5. KinematicCharacterController.KinematicCharacterSystem:FixedUpdate () (at Assets/KinematicCharacterController/Core/KinematicCharacterSystem.cs:134)
    6.  
     
  28. slonersoft

    slonersoft

    Joined:
    Feb 7, 2015
    Posts:
    11
    I’m having an issue where creating an NPC with a motor via Instantiate causes the NPC to appear in a different location near the origin for one frame. Anyone seen this before? I removed all functionality from the component and discovered it was KinematicCharacterMotor.

    Does @philsa-unity still support this package?
     
  29. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    209
    @slonersoft
    Don't ping him like this bro, he's now a Unity dev, got hands full now.
    This package is now handed to community, we support ourselves.

    Regarding your problem, you can always teleport motor to your desire position using Motor.SetPositionAndRotation(position, rotation);
     
    Gooren likes this.
  30. slonersoft

    slonersoft

    Joined:
    Feb 7, 2015
    Posts:
    11
    @Spikebor I see he's got an ECS version he's developing internal to Unity, so I'm gonna see about switching to that. For now, your fix works great for me. Thanks!

    I didn't even notice the _unity on his username at first. I wrote it in, saw his avatar was the same, and auto-completed what it gave me. Congrats to him! He makes good software.
     
    Last edited: Mar 27, 2023
    Gooren and Spikebor like this.
  31. kana1939

    kana1939

    Joined:
    Jan 4, 2022
    Posts:
    6
    I implemented jumping using state and ForceUnground, but I don't want the alignment to the ground feature. Can I disable it separately?
     
  32. fjf1889

    fjf1889

    Joined:
    Oct 30, 2021
    Posts:
    2
    animation driven rootmotion jump. I cant controll the y axis movement by animation. Can any one help how can i implement it? I use this code in UpdateVelocity(): currentVelocity = _rootMotionPositionDelta / deltaTime.but It doesn't work very well.(i doesn't bake Y)
     
  33. alexanderFabula

    alexanderFabula

    Joined:
    Sep 1, 2019
    Posts:
    22
    Im also looking for a solution where I can apply the velocity of moving platforms moved by PhysicsMover (in a timeline) to the character when they are climbing on a ladder attached to a moving platform.
     
  34. brummer

    brummer

    Joined:
    Jul 3, 2013
    Posts:
    31
    Has anyone ran into this issue before?
    I'm copying the same code between two projects, and on one of them, it seems like the controller's collider's position does not match the position of the motor of the transform, when debugged.
    Which is weird, because in the editor view, the collider does indeed seem to be in the correct place.
    Nothing fancy is being done, it's pretty basic code relative to the examples without any in-depth modification.
    Seems like the collider's position is just staying where it is at the start, and if velocity is applied to the controller, the motor and transform moves, but the collider stays in the same position when debugged with bounds.center. They're all on the same transform.