Search Unity

Move in place motion a few steps each button press

Discussion in 'Animation' started by AndyNeoman, Sep 14, 2017.

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Hi all,

    I'm looking for the simplest way to move character a few steps the he makes in an animation. Inplace boxing attack.

    He steps forward around a metre in the 1 second clip. I've tried Rigidbody.moveposition, tried addforce and transform translate but its difficult finding the correct amount. He either moves too far, teleports at the beginning of the animation or stays in place.

    Is there any tips or trick to do this?


    Thanks

    Andy
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Have you looked into generating root motion for the character? There is a video tutorial in the learn section on how that is done.
    Beyond that - the best process is to create the animation you want externally in a 3D app. This might not be what you want - but it is the best process regardless.
     
    AndyNeoman likes this.
  3. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    I already have the animations. The character controller is almost done, he moves, rotates does some specific functions for creating a nest, eating, interacting etc.. So don't really want to change the controller to root.

    I just have a couple of attacks that don't really work properly. Is it really tricky to create a movement for each animation play that will move the char a set distance like two strides? I know it is for me at the moment lol :) I just thought I was missing something simple.
     
  4. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    thanks @theANMATOR2b , I took another look at the scripting root motion page and I have made some headway.

    The character is moving quite believable now (curves are quite beautiful) I got one slight issue to crack as when I move the player the motion's rotation seems to change.

    Originally the animation moves the player forward but after I have controlled the player the animation has swung around and it is moving him the wrong way. I don't suppose you have any idea how to correct that?

    Thanks again for your help.
     
  5. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    So I think I worked out the issue. The unity docs scripting tutorial does not account for the rotation of the player so as soon as the player changes rotation the motion is not the correct direction. https://docs.unity3d.com/Manual/ScriptingRootMotion.html

    Can any quaternion experts help how to account for it in this function?

    Code (CSharp):
    1. Vector3 newPos = gorillaMeshTransPos;          
    2.             newPos.z += anim.GetFloat("RootMotion") * Time.deltaTime;
    3.             transform.position = newPos;
     
  6. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    I'm not a coder so I can't assist beyond suggesting where to look for help. I'm glad your making progress. Please update with your final solution once you find it.
     
    AndyNeoman likes this.
  7. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    The solution was so simple it hurts......

    1. Vector3 newPos = gorillaMeshTransPos;
    2. newPos.z += transform.forward * anim.GetFloat("RootMotion") * Time.deltaTime;
    3. transform.position = newPos;
    (transform.forward just needed to be added to my script.) works as intended now.
     
    theANMATOR2b likes this.