0
\$\begingroup\$

Implementing the suggestions from this answered question, I am trying to move my player according to the animation. To do so, I am adding the transform.position property to the animation and then, considering only an horizontal left movement just for testing, I modified the curve like this:

enter image description here

By properly tuning the curve, I managed to move the character following the animation, however I am having two main problems:

1) As soon as I add the position property to the animation, the character does not move anymore, even if the animations are correctly showing up. My first thought is that my modifying the position I am actually forcing the gameObject to stay still in the position that I obtain from the curve. Another possibility may be a problem with my Move method:

private void Move()
{
    moveInput.Normalize();
    myRigidBody.MovePosition(myRigidBody.position + moveInput * moveSpeed * Time.deltaTime);
}

With MoveInput a 2d Vector storing the x and y value from my inputs and moveSpeed a float. Could it be that RigidBody2D.MovePosition is somewhat conflicting with the animation curve?

2) Even if the position changes during the animation, it goes back to the starting place once it ends, whereas I want the position to be updated with the final position assumed during the animation.

A solution using the animation interface would be great, but I don't mind one through scripting, I was looking for it initially but I am not understanding how to call a precise animation in a script and modify its properties.

Any hint would be much appreciated, cheers!

Update: I managed to make two simple functions to be called from the animation event that take a float as input to move the player either horizontally or vertically:

public class Animation_Tuning : MonoBehaviour
{
    private Rigidbody2D myRigidBody;

    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();
    }
    public IEnumerator Animation_MoveHorizontal(float step)
    {
        Vector2 direction = new Vector2(step, 0f);

        myRigidBody.MovePosition(myRigidBody.position + direction);
        yield return null;
    }

    public IEnumerator Animation_MoveVertical(float step)
    {
        Vector2 direction = new Vector2(0f, step);

        myRigidBody.MovePosition(myRigidBody.position + direction);
        yield return null;
    }
}

However since the animation events accept only functions with 0 or 1 parameters, I find it rather limiting to use them in place of the curves, so if somebody can think of a solution please let me know!

\$\endgroup\$
  • 1
    \$\begingroup\$ The first question has been answered before many times. The animator can't work with delta values, so you need to put the sprite of the player inside the player hierarchically. You can move the outer container with a script and put the animations on the sprite. \$\endgroup\$ – Bálint Jun 6 '19 at 9:21
  • \$\begingroup\$ Thank you for the comment, I have actually manage to edit those two methods and using AddForce instead of MovePosition and it is much better, I will edit the question to leave the remaining problem! \$\endgroup\$ – FSic Jun 6 '19 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.