Search Unity

[RELEASED] Easy Character Movement

Discussion in 'Assets and Asset Store' started by Krull, Apr 21, 2016.

  1. EpicMcDude

    EpicMcDude

    Joined:
    Apr 15, 2013
    Posts:
    117
    I just mentioned the timescale issue because it's a similar issue, but that one was already fixed as I mentioned with that mod to your code, I didn't mean to confuse you so nevermind this, sorry!

    The only problem I'm having is being able to reattach mouse control smoothly to the last point the camera/player object was rotated towards.
    Example:


    I press a key to remove mouse control (canNotRotate = true, as can be seen in the code screenshot in my previous post) and then use the smooth look at function to rotate the camera and/or player object towards another object. Once I renable the RotateView method it snaps to the last point before I took away control, as can be seen in the video.

    So taking away control works fine, a bool check prevents the mouse from being used to rotate the player object, I just need to have RotateView renabled and continue from where the player object got rotated towards by the smooth look at function.
     
  2. RickZhao96

    RickZhao96

    Joined:
    Nov 1, 2020
    Posts:
    2
    Hi hello, thank you very much for making this plugin, the neatness of the code is amazing. :)
    But I found a strange problem in use, please let me describe it to you:

    First of all, since I am going to make a multiplayer game, the network framework uses PUN, but PUN does not support new inputsystem, so I choose another plugin of yours: "Character Movement" version is 1.0.6, I am in windows Run your CM plug-in in , everything is fine, but after testing, I found that in the macOS or web environment, when the character first moves and turns to the direction the camera is facing, there will be a strange back and forth jitter phenomenon, similar to the diving board being pressed Back to straight vibration phenomenon after bending.
    It's a problem with the ThirdPersonController in the demo scene.

    Then I checked your ECM2 plug-in and found that the third-person controller in the ECM2 plug-in does not have this problem, and then I found that the core component CharacterMovement script in your CM plug-in is exactly the same as the script in ECM2, then the problem should appear in In the ThirdPersonController in CM, I only know a little about C#, so I would like to ask you what is the reason for this magical problem and how to solve it. Once again, thank you very much.

    Looking forward to your reply
    Have a nice day!:)

    ps: English is translated by Google;)
    MacTest.gif
     
  3. RickZhao96

    RickZhao96

    Joined:
    Nov 1, 2020
    Posts:
    2

    Hi there, today I studied carefully and found out where the problem occurred. When I put updaterotation() in CM's thirdpersoncontroller; The function is moved from onlatefixedupdate() method to update() method or up line from updatecamera() in lateupdate() method; This strange problem will be solved in the above method. It is similar to the two pictures, but I still don't know why and what caused this problem. I'd like you to explain it to me, and I'd like to ask you how the following adjustments will affect the performance:D
     

    Attached Files:

  4. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @EpicMcDude

    Thank you for your clarification.

    I think your issue could be due to how you rotate the character / view when you remove mouse control. I mean the First Person controller handles rotation in a particular way, the yaw rotation (around character's Y-Axis) is handled by the character (i.e: movement.rotation) while the pitch rotation is handled by the camera's transform (X-Axis).

    So when you take aways mouse control you introduce a desync between First Person rig and your rotation since the MouseLook script caches the initial rotation(s) and use this to update rotations (mostly smooth method) so when you give back mouse rotation control it continues from its last state (its cached values).

    An easier way to solve your issue is calling the MouseLook Init function when you are about to give mouse look control using your up-to-date values, this will re-init its cached values with your up to date ones.

    Alternatively if you are not using the MouseLook smooth option you can simplify the MouseLook LookRotation function to always use its up-to-date values ignoring the cached ones, as follows:

    Code (csharp):
    1.  
    2. public virtual void LookRotation(CharacterMovement movement, Transform cameraTransform)
    3. {
    4.     var yaw = Input.GetAxis("Mouse X") * Time.timeScale * lateralSensitivity;
    5.     var pitch = Input.GetAxis("Mouse Y") * Time.timeScale * verticalSensitivity;
    6.  
    7.     var yawRotation = Quaternion.Euler(0.0f, yaw, 0.0f);
    8.     var pitchRotation = Quaternion.Euler(-pitch, 0.0f, 0.0f);
    9.  
    10.     movement.rotation *= yawRotation;
    11.     cameraTransform.localRotation *= pitchRotation;
    12.  
    13.     if (clampPitch)
    14.         cameraTransform.localRotation = ClampPitch(cameraTransform.localRotation);
    15.  
    16.     UpdateCursorLock();
    17. }
    Let me know if I can help you any further.

    Regards,
    Krull
     
    EpicMcDude likes this.
  5. EpicMcDude

    EpicMcDude

    Joined:
    Apr 15, 2013
    Posts:
    117
    Ah that's it! That was the fix, thank you very much Krull!
     
  6. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Awesome! glad I was able to help you.

    Regards,
    Krull
     
  7. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @RickZhao96,

    First of all, thank you for purchasing ECM2.

    About your questions, yes ECM2 uses CharacterMovement as its character controller but adds a high level layer on top of it, i.e. the Character(s) class.

    Honestly I have not noticed the issue, so in order to give you a definitive answer I'll need to perform some tests, however you should not worry about the performance, it is very optimized and updating rotation functions is pretty lean.

    Another option to check is if your character is being interpolated (Rigidbody interpolate) as this could explain why calling it on Update method fixes it.

    In the meantime, if you have any further question(s), please let me know.

    Regards,
    Krull
     
  8. EpicMcDude

    EpicMcDude

    Joined:
    Apr 15, 2013
    Posts:
    117
    Hey Krull,

    I just keep coming back to annoy you, sorry about that! I was wondering if you could point in the right direction for this problem.

    Is there a way to have mouse look only rotate the camera and keep the Player capsule in a fixed rotation? So we can only have the "head" move and not the body rotate with it.

    I tried using my own mouse look script when I need it active, and it worked to an extent, but once I put control back to ECM Mouse Look, the camera gets misaligned with the Player's capsule forward direction causing the camera to look one way and the Player capsule to look another way.

    Thank you
     
    Last edited: Aug 11, 2022
  9. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @EpicMcDude,

    I think you can use the same approach used in the smoother fps example, where the mouse look rotates the camera pivot instead of the player's capsule, so the capsule's yaw will remain unchanged.

    I attached a working example of it and should help you to solve the problem.

    Regards,
    Krull
     

    Attached Files:

    EpicMcDude likes this.
  10. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Hi oscar I tried to contact you via mail but didn't get any answer.
     
  11. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @Mars91,

    Sorry for the late reply, I definitely missed your email. Please contact me to email or if you prefer, post it here.

    Looking forward to hearing from you,
    Oscar
     
  12. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Can you share you email again maybe I got it wrong.
     
  13. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Sure, its ogracian@gmail.com the same from docs or publisher profile page.

    Regards,
    Oscar
     
  14. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    eMail sent
     
  15. Amo-deus

    Amo-deus

    Joined:
    Jul 27, 2015
    Posts:
    25
    Is there a way to get the character to move on logs like this?
    @ 0:10 second mark?? I've had luck in the past using simple physics based characters and physics materials, but I cant seem to make it work with ECM.
     
  16. socialtrens

    socialtrens

    Joined:
    Oct 23, 2017
    Posts:
    65
    You need to use Kinematic Character Controller (for GameObject) or com.unity.charactercontroller (for DOTS). Both are made by the same dev. Both of them support walking on moving object out of the box. If you want to make 3D platformer game, I recommended you to use KCC or official CC for DOTS. They are both free.

    At first, I use ECM too, but then I realized that ECM doesn't support walking on moving object out of the box. That's why I use KCC instead.
     
  17. Amo-deus

    Amo-deus

    Joined:
    Jul 27, 2015
    Posts:
    25
    Got it to work! I needed at add a downward force to the player, as well as adding physics materials.
     
    Krull likes this.
  18. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi Amo-deus,

    Glad to hear you were able to solve it!

    Yes, the controller by default uses a no friction material so it can slide along surfaces, so in your case your solution is correct as you are forcing the physical interaction between the character's rigidbody and the log.

    Last but not the least, in case you (or others) decide to go the kinematic route I suggest ECM2 or if you are just willing to replace Unity's built-in cc the CharacterMovement (used by ECM2) component, a very robust kinematic character controller with many advantages and features over both built-in cc and KCC.
     
    Amo-deus likes this.
  19. Amo-deus

    Amo-deus

    Joined:
    Jul 27, 2015
    Posts:
    25
    Thanks for the reply. I actually already have ECM2! My project is built around ECM though, so it would be a lot of work to change it.I've added a ton of stuff thats not part of the original ECM.
     
  20. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Yes, that's a good decision, after all ECM is very capable and if it's working for your project no need to upgrade, more if you already have done modifications, however if starting a new project from scratch I would suggest ECM2 if possible :)
     
  21. Paul-van-der-Laan

    Paul-van-der-Laan

    Joined:
    Oct 12, 2013
    Posts:
    60
    Hey Krull,

    I'm using ECM for a Game Jam and so far it is indeed very easy to use!

    I wonder if you have advice on how to add sound to for example the jump and mid-air jumps. There are no events I can hook into and overriding the Jump and MidAirJump methods is also not ideal as some variables are private (such as _jumpPreGroundedToleranceTime).

    I'm using a simple character with only 3 looping animations (walk, idle, jump/falling) , so I can't use animation events.
     
  22. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @Paul-van-der-Laan,

    First of all thank you for choosing ECM, and happy to hear you like it!

    Mmm, well if modifying the source code is not an option, you can use the properties, like jump, isJumping, midAirJumpCount etc, to handle these cases.

    For example, in your jump input you can trigger the sound for the jump, and check the midAirJumpCount to handle the 'in-air' case, as this is incremented each time an in-air jump is performed up to the max mid air jumps defined.
     
  23. WryMim

    WryMim

    Joined:
    Mar 16, 2019
    Posts:
    69
    Thanks for the great asset! There are a couple questions.
    1. Do you plan on updating it? There are no updates since 2021
    2. What is the best way to add footstep sounds (first person game, no animator)
     
  24. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi@WryMim,

    Thank you! Happy to hear you like it.

    About your questions:

    With the release of ECM2 and the Character Movement packages, the current ECM1 status is "Maintenance" ; it means it gets full customer support and bug fixes updates, but currently has no planned new features in a short term.

    Well, my suggested way is animating the camera (Animator) and using Animation Events since this will make it pretty easy to get camera / footsteps SFX in sync.

    If that's not an option, you could trigger the sfx at a fixed time interval (timer, co-routine, etc) only while the character is moving. For example:

    Code (csharp):
    1.  
    2. public strideLength = 0.65f;
    3.  
    4. private float _traveledDistance;
    5.  
    6. protected override void Move()
    7. {
    8.     base.Move();
    9.  
    10.     if (isGrounded)
    11.     {
    12.         // Character current speed
    13.  
    14.         float currentSpeed = movement.velocity.magnitude;
    15.  
    16.         // Increase traveled distance
    17.  
    18.         _traveledDistance += currentSpeed * Time.deltaTime;
    19.      
    20.         if (_traveledDistance >= strideLength)
    21.         {
    22.             // Reset traveled distance and play footstep sfx.
    23.  
    24.             _traveledDistance = 0.0f;
    25.  
    26.             Debug.Log("Play footsteps SFX");
    27.         }
    28.     }
    29. }
    30.  
    Let me know if you have any further questions.
     
    Last edited: Jul 29, 2023
    WryMim likes this.
  25. WryMim

    WryMim

    Joined:
    Mar 16, 2019
    Posts:
    69
    Can you tell me the best way to implement player flight? Like super man that would fly forward and he could be controlled?
     
  26. Krull

    Krull

    Joined:
    Oct 31, 2014
    Posts:
    772
    Hi @WryMim

    To enable flying you'll need to set the allowed vertical movement to true and adjust the air control to improve in air mobility. Once flying, you'll need to adjust the movement direction accordingly. I suggest you take a look at the included flying example (walkthrough) for a working implementation.

    Now in order to rotate the character as he flys forwards, I think the easiest way is using root motion, where the animation handles the moving forward, and with no input, the idle animation restores the character to a vertical floating pose, very similar to swimming. I don't want to use root motion, you'll need to rotate the character based on movement input from floating (standing) to prone (flying).