How do you change a players position during an animation?

Hello, I’ve come across a problem recently. I’m making a endless runner. In my running animation i want him to go up and down so in my animation i used the move tool to make him go up in a part of the animation. But when i do that it of course messes up the game and my guy wont move. I believe this happens because the animator is interfering with the player controller when you move the position of the player. Someone had a similar problem and suggested making an empty parent object and putting the player controller on that. But that wont work because I think my script needs to find my players collider and rigid body. If anyone could tell me a solution to this problem by telling me how i could modify my script when its on the players parent object to make it work or another solution it would be greatly appreciated. Here is my script:

public class PlayerController : MonoBehaviour

{
public float movespeed;

public float jumpforce;

private Rigidbody2D myRigidbody;

public bool grounded;

public LayerMask whatIsGround;

private Collider2D myCollider;

private Animator myAnimator;

// Use this for initialization

void Start()
{
myRigidbody = GetComponent();
myCollider = GetComponent();
myAnimator = GetComponent();
}
// Update is called once per frame

void Update()

{

grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);

myRigidbody.velocity = new Vector2(movespeed, myRigidbody.velocity.y);

if (CrossPlatformInputManager.GetButtonDown(“jump”) || Input.GetMouseButtonDown(0))
{

if (grounded)

{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpforce);

}
}

myAnimator.SetFloat (“Speed”, myRigidbody.velocity.x);

myAnimator.SetBool (“Grounded”, grounded);

}

}

Thank you for your help.

just move the player when you are recording the animation and it will be apart of the animation. But if you want to control movement with code I think you have to tick or untick root motion box.

Someone had a similar problem and
suggested making an empty parent
object and putting the player
controller on that. But that wont work
because I think my script needs to
find my players collider and rigid
body.

Someone was correct, this is the best way of doing it. You can attempt to workaround but it will probably lead to headaches down the line. In principle you should avoid animating a gameobject which has an active rigidbody.

You can still get the collider and rigidbody. In fact you can get any component on any gameobject you want from any open scene in any script. myRigidbody = GetComponentInParent(Rigidbody()); etc. would probably work in this case.