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.
  2. Dismiss Notice

Help with Character Movement?

Discussion in 'Scripting' started by deen0_, Jan 6, 2022.

  1. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Hello, how are you guys.

    So I recently just swapped character models, but now my player movement (using velocity) seems to need a little fixing. It used to work fine on my old model, but as I swapped, the new model only seems to move forward, and not in any other direction.

    Any advice? I did make sure to match components.

    Let me know of any additional info that could be of help.

    I have Apply Root Motion on, does that mean it could be an issue originating from the movement animations? Animations are from mixamo right now, it seems that they are imported as an .fbx first, to which I sourced my previous character model, this is most likely it.

    TLDR:
    Im having an issue when I try and swap player models. It seems that it only moves forward now? I was thinking that it might be an issue with root motion, since my animations are from mixamo based on my previous player model. Does this seem plausible or is there another explaination? I think this is the problem because my forward run motion is the only motion which I outsourced outside of mixamo.
     
    Last edited: Jan 6, 2022
  2. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Move function:
    Code (CSharp):
    1. private void Move()
    2.     {
    3.         // Grounded
    4.         isGrounded = Physics.CheckSphere(groundCheckObject.position, groundCheckDistance, groundMask);
    5.  
    6.         if (isGrounded && velocity.y < 0)
    7.         {
    8.             velocity.y = -2f;
    9.             // Sticks us to the ground
    10.         }
    11.  
    12.  
    13.  
    14.         Vector3 inputDir = transform.right * moveDirection.x + transform.forward * moveDirection.y;
    15.         inputDir = inputDir.normalized;
    16.        
    17.  
    18.         // Not moving if we not grounded
    19.         if (isGrounded)
    20.         {
    21.            
    22.  
    23.             // Sprint
    24.             if (canSprint && wantToSprint)
    25.             {
    26.                 speedMultiplier = sprintMultiplier;
    27.                 //moveAmount *= sprintMultiplier;
    28.                 canDodge = false;
    29.             }
    30.             else
    31.             {
    32.  
    33.                 canDodge = true;
    34.             }
    35.  
    36.             // Dodge
    37.             if (canDodge && wantToDodge && !justDodged && moveDirection != Vector2.zero)
    38.             {
    39.                 speedMultiplier = dodgeMultiplier;
    40.                 //moveAmount *= dodgeMultiplier;
    41.                 canSprint = false;
    42.                 gameObject.GetComponent<PlayerAttack>().canAttack = false;
    43.  
    44.                 StartCoroutine(Dodge());
    45.             }
    46.             else
    47.             {
    48.  
    49.                 canSprint = true;
    50.                 gameObject.GetComponent<PlayerAttack>().canAttack = true;
    51.             }
    52.  
    53.             if (!wantToDodge && !wantToSprint)
    54.             {
    55.                 speedMultiplier = initSpeedMultiplier;
    56.             }
    57.  
    58.             moveAmount = inputDir * moveSpeed * speedMultiplier;
    59.         }
    60.  
    61.         moveAmount.y += gravity * Time.fixedDeltaTime;
    62.         //Debug.Log(moveAmount);
    63.         gameObject.GetComponent<Rigidbody>().velocity = moveAmount;
    64.     }
    Player components:
     

    Attached Files:

  3. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    Hi! You may want to step by step check which part leads to the problem.
    e.g.
    Input problem? Debug.Log(moveDirection)
    Animator? Check the parameter you use.. and so on.
     
  4. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Input system seemed fine, since the last model worked fine.

    I’m figuring it was an animation/root motion issue, currently just redoing the movement, step by step and hopefully using proper compatible animations.
     
  5. Deleted User

    Deleted User

    Guest

    You should post all relevant code and not just the move method in order to rule out any other pieces of code that might be causing your issue.

    For example, you're directly modifying the velocity of a rigidbody which is a bad idea in 99.99999% of cases as it can lead to unexpected physics results.

    This could also be compounded based on where you invoke from (Update vs FixedUpdate).


    This could potentially be the issue. I've had issues mixing animations from mixamo and non-mixamo sources in the past. Unfortunately to figure that out requires more information such as the animation file settings.

    Compare the non-mixamo animation to the equivilant mixamo animation in Unity and you will probably find the default settings in the inspector window are different (you don't mention anything about setting them after first importing the animations).
     
  6. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Could you explain further on not modyfying velocity? I thought RigidBody calculations were best if wanted to work with collisions, etc.
     
  7. Deleted User

    Deleted User

    Guest

    It's mentioned in the documentation. There are reasons to do it but not directly modifying velocity is more of the default behaviour. ie - you should only directly modify it when required rather than all of the time.

    https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour - use AddForce instead Do not set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical usage is where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity."
     
    deen0_ likes this.