Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[RELEASED] Easy Character Movement 2

Discussion in 'Assets and Asset Store' started by Krull, May 5, 2021.

  1. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    Hello Krull, is there anyway to change the orientation of the controller? I am trying to do swimming but the character is horrizontal rather than standing because he is swimming. However because of this the collision looks weird because the orientation of the collider is not horrizontal as well.
    Also what does the buoyancy do? No matter what number I set it too it seems to not make a diffference. I thought it was to stop the character sinking like a stone but it's not stopping that...
     
    Last edited: Nov 4, 2023
  2. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Sapien_

    For this you'll need to manually rotate the character as your game needs, in this case I think using root motion is the best approach, letting the animation handle the floating - swimming forward orientation transitions.

    The buoyancy needs a physics volume to operate, once inside it, it computes the character immersion depth, and adjusts the gravity acting on the character based on your buoyancy value and the immersion depth.
     
  3. chumomo

    chumomo

    Joined:
    Jun 30, 2017
    Posts:
    17
    Hello, thanks for your great work again!
    I want to implement a function that
    when I click on the wall, a new character will spawn at the clicked point of the wall. The character can walk perpendicular to the walk rather than fall off the wall.
    I tried modding flying mod but the result was unsatisfying.

    My question: Is there a simple way to keep the character walking on the wall as on the ground?

    Thanks!
     
  4. Sapien_

    Sapien_

    Joined:
    Mar 5, 2018
    Posts:
    102
    I can't use Root motion due to the unique nature of the character animation.
    Seems I didn't need the buoyancy. May revisit it later but I think the basic character swiming is quite nice to use.
    I should probably explain more. I want the character to also be able to swim up and down (I am using what would normally be the jump button to ellevate and what would be the crouch button decend), Right now I am using launch character every frame at a low value. But it doesn't "feel" good. what would you suggest? I want the character to swim up or down when not pressing a direction and swim diagonally up/down, when moving the control stick.
    Maybe I can make the character mesh rotate up/down, get that Y direction and add it to the Y of the move vector?
     
    Last edited: Nov 7, 2023
  5. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Sapien_,

    For this, you should modify the character input when swimming, so you add vertical movement (up/down) to the given movement direction. I suggest you take a look at the Flying example (Easy Character Movement 2\Examples\5.- Gameplay\5.7.- Fly) , as actually the approach is basically the same, override the HandleInput and adjust the movement direction based on your character's logical state, in your case, swimming.

    My root motion sugestion was to let it handle the capsule rotation, instead of movement, even more when buoyancy is used, but obviously its not a must ;)
     
  6. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @chumomo,

    Thank you for your kind comments!

    About your question:

    Yes, you'll need to rotate the character so it is perpendicular to the wall, when rotated the character will 'see' the wall as its ground, since the calculations are with respect to character up. Additionally you will also need to rotate the gravity otherwise the character will fall due gravity force acting in a different / unwanted direction.

    One key factor, is to force a ground state update when you spawn your character, for this, use the CharacterMovement SetPosition, ie:

    characterMovement.SetPosition(transform.position, true);

    without this the Character will pass to falling state first, since the grounding state is updated until the next CharacterMovement.Move method call.
     
  7. BlxsdGames

    BlxsdGames

    Joined:
    Jul 19, 2022
    Posts:
    4
    Beautiful controller. Could you help with a fusion FPS style camera? When another client connects that client cant move their camera much and the client players camera moves with the host even after setting each camera on Spawned(). Would I need to make the camera input be read though the PlayerInput class even if I don't care to replicate the cameras to multiple clients or could that stay local? Seems both players are trying to access the camera at the same time how would you combat this?
     
    Last edited: Nov 9, 2023
  8. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758

    Hi @BlxsdGames,

    Happy to hear you like ECM2!

    About your question:

    The included example synchronizes only the base Character class, so these issues are expected when using a derived class like the First Person Character, since some additional used data is not replicated.

    First, the camera needs to be assigned once the character has spawned so only the authority takes control over it, as fusion docs comments, There are two common ways to achieve this.
    1. When the local player object gets spawned, have it instantiate a camera.
    2. Have the camera as part of the scene. When the local player object gets spawned, have it find the camera and set the target of the camera to it.

    For the input part, yes, you need to add the mouse input (yaw, pitch) to the player input just like other inputs, so only the authority operates over this. like with the movement ,jump, etc. I suggest you take a look at this video as it shows the basic setup for a first person using photon and its character controller prototype, and definitely should help you get a better overview of it and then apply it to ECM2

    Having said that, I have planned to add a First Person Example as part of the upcoming update (currently in development).
     
  9. J2CD

    J2CD

    Joined:
    Jul 28, 2021
    Posts:
    3
    I'm using your ECM2 asset (specifically "Character.cs") alongside Photon Fusion, however I'm having some trouble expanding the player character from the ECM2 example.

    My current approach has a root player that holds all the scripts pertaining to moving the character (as from the example).

    upload_2023-11-9_0-28-2.png

    The objects Local and Remote are enabled/disabled depending on if the player is local or not. This works fine for the moment.

    I wanted to see if I could move all the logic for moving the player (and other should-be-local behaviour) into the local object, thus disabling the logic when the player is not local.

    This would mean that the remote player seen by a local player would not have any active scripts, as it would be akin to a empty husk. This would, in my hopes, prevent any messy behaviour by having duplicate scripts on different players.

    In practice, I obtain an odd error which I can't seem to understand:

    "Rotation quaternions must be unit length."

    Located in "CharacterMovement.cs" on line 4751.

    Rather than helping me fix a rather vague error message, I'd prefer if we could discuss how you yourself would go about implementing this.
     

    Attached Files:

  10. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @J2CD,

    In general when doing server client side prediction with server authoritative movement at great sight, it boils down to send local player input to the server along with your character's state, so the server reproduce this movement and issue a correction in case your results are different from the server (the REAL ones), when this error occurs, the local player is resimulated from the corrected server data, due this re-simulation the client and server must be able to reach the same results using the same input data, otherwise desyncs occurs.

    Now, regarding your specific case, by Local and Remote do you refer to visuals only (ie: models?) or are you aiming for a behaviour similar to what unreal engine uses, where characters / actors are handled differently based on its current network role (ie: authority, autonomous proxy, simulated proxy)?

    To my understanding, photon already handle characters based on its role, similar to the unreal approach commented above, when your character uses auto as the interpolation data source, where prediction and resimulation occurs only on the local player (autonomous proxy in Unreal) and this character will be a mere interpolated position/rotation on a remote computer (simulated proxy in unreal)

    The specific error message is due a invalid rotation in the networking state.

    The part I don't fully understand is your local / remote objects, could you elaborate a bit about this?
     
  11. J2CD

    J2CD

    Joined:
    Jul 28, 2021
    Posts:
    3
    Thanks for the response,

    It seems like there was a misunderstanding on my end on how local users move.

    I had assumed that the local users themselves were responsible in their session using the player controller in order to move. Instead, if I am understanding this correctly, the local user sends input to the server, which then acts on the player on the server session which is synced over the network to the local sessions.

    My original idea of local and remote objects was based on my misunderstanding, and I had hoped to prevent overlapping behaviour (e.g., a script in the local session causing an effect on the local player, but then also repeating that effect on the server session). I see now that a script in the local session would have no effect unless it requests the server beforehand.

    I believe my misunderstanding stems from the current issue that prompted by original idea. My player moves relative to the main camera using the "relativeTo" vector function. For some reason, the local player moves relative not to its main camera, but the servers main camera.

    I will try troubleshooting this in other ways now.

    Edit: The cause seems to be that the server calculates the local players relativeTo direction using the servers main camera, whereas it needs to use the local camera. I need to find some way of changing the player in the server session so that it uses the correct vector.
     
    Last edited: Nov 9, 2023
  12. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Ah I see, well Photon Fusion also supports Shared Mode which is similar to your approach and more like a local player and more similar to PUN if you have experience with that. However the included example uses the hosted / server mode which while a bit more difficult to code, offers many advantages other than shared mode, but in the end is up to the game to decide which approach fits the best.

    My player moves relative to the main camera using the "relativeTo" vector function. For some reason, the local player moves relative not to its main camera, but the servers main camera.

    As previously commented the camera should be assigned when the player is spawned so the local player takes control of it.

    Let me know if I can be of further assistance or feel free to contact fusion guys, they are very kind and surely will help you too!
     
  13. Server12

    Server12

    Joined:
    Jul 3, 2012
    Posts:
    1
    Hi @Krull,
    I want to implement push/pull boxes,constrained by X or Z only, i'm use fixed joint for this, and when i pull all ok, but push not work,can you help me with this?
     
  14. Rollergoose

    Rollergoose

    Joined:
    Jul 18, 2018
    Posts:
    6
    Hi!
    Thinking about buying this asset, but wonder if:
    Is there a way to use a gravitypoint on a platform and have the player follow it? For example a player walks inside a airplane that moves and rotates, I dont want the player to be facing vector.up i want it to follow the airplanes rotations (but not the ground since it can be stairs inside the player walks on).

    thx in advance.
     
  15. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Server12,

    A Character can push others (rigidbodies or characters) by default but need to be enabled (physics interactions, push characters, etc).

    One key point is the character controller is kinematic, this means it won't move unless we allow it and handle it! About your particular case, honestly I have not tested so I appreciate it if you could provide me further information so I get a better perspective.

    Having said that, you can try using the CharacterMovement SetPlatform method (soon be renamed as AttachTo) this makes a Character carry the 'parent' movement / rotations (ie: moving platforms) but without it needing standing on top of it, and think it could help in your case.

    Also it's important to make sure your character does not collide with the 'other' while jointed (ie: grabbed object), otherwise it could be the reason your character can't move. You can use the Character IgnoreXXX functions for this.
     
  16. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Rollergoose

    First of all thank you for your interest in ECM2!

    About your question:

    Yes, while it can by default seamlessly handle the 'platform' movement and rotation for you, you will need to update the character's up manually in order to follow the airplane changes.

    It includes a 'Planet Walk' Example similar to mario galaxy, where it shows how to adjust the character's up and gravity direction, in this case following the planet orientation and it should help to achieve your use case.
     
  17. Rollergoose

    Rollergoose

    Joined:
    Jul 18, 2018
    Posts:
    6
    Hi. Thanks for the answer. Yes bought the asset before your answer and saw that and figured out what to do. So thanks for the awesome docs! Been trying to figure out if I can implement so the player dont affect the airplanes physics when inside it, for example if the plane is rotating and player walks in to an object inside it the rotation is affected. First time using kinematic. My solution before was to have all the physics on another object that then the visual mirrored. But wanted to try out this method and looks more clean if It works.

    Do you have any recomendations? Or should i look up to have the airplane also as a kinematic?
    Thanks in advance once more.
     
  18. ErgoAaronSum

    ErgoAaronSum

    Joined:
    Mar 6, 2017
    Posts:
    8
    Hello, I am looking at the character controller events and I am trying to play an animation based on if the player is sliding down a slope. Is there any event callback or exposed variable I can use to feed my animation system?
     
  19. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Rollergoose,

    First of all, thank you for purchasing ECM2!

    About your question:

    You can try disabling the physical collision between your airplane and the character's capsule, because as you already noticed if the character collides with the plane the physx engine will handle the collision, for this you can use the included CapsuleIgnoreCollision function, this is a wrapper for the Physics.IgnoreCollision and see if this is enough.

    If not, I think the best approach could be to make your plane kinematic too so you have full control over it and your character
     
  20. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @ErgoAaronSum

    Do you mean, sliding down due to it being not walkable? It is not exposed, however you can query the character's data as needed, for example in this case you can check:

    Code (CSharp):
    1. if (character.IsOnGround() && !character.IsOnWalkableGround())
    2. {
    3.     // Character is sliding off a non walkable surface...
    4. }
    or using Character movement modes:

    Code (CSharp):
    1. if (character.IsFalling() && character.IsOnGround())
    2. {
    3.      // Character is sliding off a non walkable surface...
    4. }
    Both are equivalent.
     
    Last edited: Nov 24, 2023
  21. ErgoAaronSum

    ErgoAaronSum

    Joined:
    Mar 6, 2017
    Posts:
    8
    Thanks for the speedy reply, my use case is to play a sliding animation when a slope is too steep so I think this is exactly what I need. I'll give it a go!
     
    Krull likes this.
  22. Mythran

    Mythran

    Joined:
    Feb 26, 2013
    Posts:
    85
    Hello,
    I'm using A* pathfinding for the ai, and i want the quadrupeds to align to the terrain.
    I've tried several solution, but the rotation is a constant fail I'm having.
    I'm hopeful for some suggestions.
    How can this be achieved?

    Thank you!
     
  23. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Mythran

    By default a Character does not orient itself along a ground surface, however this can be added, for example, the script from this post, shows a way to orient a character along an area sampled normal, and should help you get started with it.

    Worth noting this is for an older ECM2 version, so some minor tweaks will be required.

    Last but not least, as part of the following update (currently in development) I am adding an updated version of this component as a new example.
     
    Mythran likes this.
  24. Mythran

    Mythran

    Joined:
    Feb 26, 2013
    Posts:
    85
    Thank you very much for the help. Will give it a try!
     
    Krull likes this.
  25. Bodyclock

    Bodyclock

    Joined:
    May 8, 2018
    Posts:
    172
    Hi, I have your Character Controller and want to know how suitable it is for an underwater 2.5D side-scrolling game where the player is an octopus. The animations are all procedural and physics-based. the character will have a slight negative buoyancy, but will have a swim and jet dash mechanic, and a walking mechanic on the sea floor. I'm assuming I can just adjust the Gravity amount on the rigidbody? Is there anything I should be aware of when using it this way? Any tips?
    Thanks for your time.
    Cheers
    Chris
     
  26. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Bodyclock

    Yes, that's possible, however for the gravity and buoyancy you should adjust those it in the Character, as it controls those values, apart for that I don't think any possible issues with your current plan, however if need further help, please let me know here on by email
     
  27. hitt312

    hitt312

    Joined:
    Jun 17, 2019
    Posts:
    1
    Hi, how can i apply bunnyhop otion to Character Controller?
     
  28. Asarge

    Asarge

    Joined:
    Jun 2, 2018
    Posts:
    26
    Which method would be best to prevent the player from slipping off large creature surfaces that would normally be walkable (ex. walking on top of a giant crab)? When the creature is moving and has a semi-flat ground-like part, it seems that the player sort of gets knocked around quite easily. Would you suggest using SetPlatform in this case?
     
  29. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @hitt312,

    I suggest you start taking the character settings, in particular friction, deceleration, etc and see if you can achieve that.

    If not, you will need to use a different formula to calculate your desired velocity, for this, you should override the Character CalcVelocity method and do your velocity calculation in there, since this method is internally used by all Character movement modes this will let you replace the default formula.
     
  30. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Asarge ,

    One of the reasons a character loses ground is due the slope angle and your character's current slope limit, I mean this is the first factor to decide if a character is on walkable ground or not. In this case you can use the SlopeLimitBehavior component.

    The SlopeLimitBehavior component lets you override a CharacterMovement SlopeLimit property allowing to define per-object behavior instead of per face. For example, a whole walkable terrain could have small faces falling out of your character's slope limit, to solve this you add the SlopeLimitBehaviour component to the terrain, and mark it as walkable.

    You can add this to your crab walking zone to prevent found non-walkable faces.
     
    Asarge likes this.
  31. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    i've seen in the documentation and stuff that I can use landedVelocity inside CharacterMovement to access how fast the character hits the ground, but it doesn't seem like that exists anywhere? How do I access that?
     
  32. KrisGungrounds

    KrisGungrounds

    Joined:
    Aug 20, 2019
    Posts:
    13
    Would you recommend this asset for a 2D platformer similar to Celeste? If yes, should we use 3D colliders or it will work also with 2D colliders?
     
  33. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    571
    This is in my experience as a long time ECM user.

    For 2D (sprites and 2D physics), I'm unsure if this will work for you.
    For 2.5D (3D models in a pseudo 2D space), it works really well!
     
    Krull likes this.
  34. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @AustinDrozin

    This value is accessed through the CharacterMovement component (aka: character motor):

    Code (CSharp):
    1. public Vector3 landedVelocity { get; private set; }
    You can use the Character characterMovement property to acces this component.
     
  35. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @KrisGungrounds

    First of all thank you for your interest in ECM2!

    About your question:

    Sure! However worth noting it is 3D based so as you commented you will need to use 3D colliders (box, sphere, capsule, etc) as it is not compatible with 2D.
     
  36. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    like this? Or am I getting the wrong thing.
    upload_2023-12-5_9-10-36.png
     
  37. Kurjenpolvi

    Kurjenpolvi

    Joined:
    Nov 15, 2016
    Posts:
    14
    Hello! I posted a question couple of months ago asking about transfer from ECM1 to ECM2. I am definitely late but better late than never, I just wanted to pop by and say that ECM2 was totally worth it! Thank you for making it @Krull !
     
    Krull likes this.
  38. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Yes, that's correct, you can use the GetCharacterMovement() method (like your example) or the characterMovement property to access the CharacterMovement component within your custom Character class.
     
    Last edited: Dec 5, 2023
  39. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @Vera987,

    Thanks for sharing your experience! Glad to hear it was worth it and that you're happy with the change. Appreciate it!
     
  40. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    View attachment 1340683
    it doesn't come up though. It's red like it shows and is an error in the console.
     
    Last edited: Dec 6, 2023
  41. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758

    Make sure you includes the EasyCharacterMovement; namespace.

    I suggest you try it in one of the Template scene custom character class, ie: MyCharacter
     
  42. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    Here are the namespaces I'm using. Am i missing one?
    upload_2023-12-8_13-13-18.png


    Made a new method inside MyCharacter First Person and it doesn't come up either.
    upload_2023-12-8_13-13-1.png
     
  43. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    571
    Are you using an older version of ECM2? Last I remember the namespace was just EasyCharacterMovement.
    using EasyCharacterMovement;
     
    Krull likes this.
  44. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    296
    on the latest version the namespace should be:

    Code (CSharp):
    1. namespace EasyCharacterMovement.Templates.FistPersonTemplate
     
    Krull likes this.
  45. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    ohhhhh so landedvelocity is just in the newer versions then?
     
  46. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi,

    Yes I think it was introduced in latest version, or prev one.

    Try this code in the TirdPerson Template Scene:

    Code (CSharp):
    1. using EasyCharacterMovement;
    2. using UnityEngine;
    3.  
    4. namespace EasyCharacterMovement.Templates.ThirdPersonTemplate
    5. {
    6.     public class MyCharacter : ThirdPersonCharacter
    7.     {
    8.         protected override void Update()
    9.         {
    10.             base.Update();
    11.  
    12.             // Access CharacterMovement through method
    13.             CharacterMovement cm = GetCharacterMovement();
    14.          
    15.             // Access CharacterMovement through property
    16.  
    17.             Vector3 landingVelocity = characterMovement.landedVelocity;
    18.             Debug.Log($"LandingVelocity: {landingVelocity:F4}");
    19.         }
    20.     }
    21. }
    This should compile without any errors.
     
  47. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    43
    does not work cus I'm on an old version, but that's fine! I'd rather not mess with updates at the moment.
     
  48. mookfasa

    mookfasa

    Joined:
    Dec 21, 2016
    Posts:
    46
    @Krull Happy New Year! Is there a way to make it so the character controller doesnt always snap to the ground when its going over sharp edges?I am looking to do something like this with the white balls.
     
  49. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    758
    Hi @mookfasa

    Happy New Year to you too!

    About your question:

    I suggest starting with a low slope limit initially. This way, on slopes, the character will essentially be in a Falling movement mode because they are standing on non-walkable ground, and the ground constraint is disabled.

    Another option, and possibly the best one, is to disable the CharacterMovement ground constraint by setting its constrainToGround property to false. You can then create a custom movement mode, providing complete freedom in determining how it should move without any kind of ground constraint.
     
    mookfasa likes this.
  50. icanhazscreenid

    icanhazscreenid

    Joined:
    Jun 2, 2016
    Posts:
    14
    @Krull How you recommend handling the situation where a characters bounding volume changes into something other than a cylider? Example: if a character picks up a crate and is holding it in front of him and then runs into a wall, crate first, how can I interface to the character controller when the crate enters a collision so that the crate doesnt go through the wall? I have the collision data and contact points from the crate. The brute force method would be to perform the dot product between each contact point and the player direction but I'm suspect this will be very robust. Thanks for your insight.