- Home /
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.
Answer by tuinal · Jul 23, 2020 at 01:05 AM
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.
Thanks for responding, but i have a problem. I switched the code to myRigidbody = GetComponentInParent();
myCollider = GetComponentInParent<Collider2D>();
myAnimator = GetComponentInParent<Animator>();
}
but the console says that value cannot be null for collider. I don't see how the value would be null if i defined it in the lines above. i think it might be because of this line of code:
private Collider2D myCollider;
i tried changing private to public and it didn't fix it. If you don't understand what my game is it just a endless runner where force is always allied to my character to make him keep moving along and jumping over stuff. If you could help me fix this problem and preferably modify my code for me it to would be appreciated. By the way I am quite new to coding and game development so sorry if some of my problems have an obvious fix.
It will be null if the function doesn't find a collider.
There could be multiple causes which are hard to guess at without the scene. The fall-back would be to simply make the fields public, and drag+drop the correct gameobjects to them (e.g. if there's a public Collider2D, you can drag + drop any gameobject with a Collider2D in the inspector and it will be assigned.
Answer by logicandchaos · Jul 22, 2020 at 09:42 PM
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.
that's the thing, you cant move the players position when recording an animation or it causes errors, my question was how do i work around this.
Your answer
Follow this Question
Related Questions
Comparing the position of two children objects 0 Answers
Create Player From Values [ Photon ] 0 Answers
How to move player after animation 2 Answers