• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by OOVUM · Jul 22, 2020 at 07:58 PM · animationmovementpositionplayerparent-child

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 $$anonymous$$m to go up and down so in my animation i used the move tool to make $$anonymous$$m 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 t$$anonymous$$s 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 t$$anonymous$$nk my script needs to find my players collider and rigid body. If anyone could tell me a solution to t$$anonymous$$s 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 t$$anonymous$$s for initialization

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

void Update()

{

grounded = Physics2D.IsTouc$$anonymous$$ngLayers(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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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 t$$anonymous$$nk my script needs to find my players collider and rigid body.

Someone was correct, t$$anonymous$$s 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 w$$anonymous$$ch 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 t$$anonymous$$s case.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OOVUM · Jul 23, 2020 at 08:42 PM 0
Share

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.

avatar image tuinal OOVUM · Jul 23, 2020 at 11:51 PM 0
Share

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.

avatar image

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 t$$anonymous$$nk you have to tick or untick root motion box.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OOVUM · Jul 23, 2020 at 09:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

391 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move player after animation 2 Answers

Movement while moving along with other object. 0 Answers

Create Player From Values [ Photon ] 0 Answers

Character faces wrong way for one frame only. Why? 1 Answer

When reversing animations via Blend Tree the game object jumps (Video) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges