Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
1
Question by Mangas · Mar 04, 2015 at 09:31 AM · c#unity 5unity 2d

Character not moving on Unity 5

I've ported a 2d platformer project into Unity 5. After finally getting the project to compile, everything works, except the basic movement for my player, it doesn't move or jump, though the animations are correctly triggered.

Here is the code for moving and jumping:

 public void Move(Direction direction)
     {
         if (CurrentDirection != direction)
         {
             ChangeDirection();
         }
 
         if (MoveAllowed(direction))
         {
             _rigidbody2D.velocity = new Vector2(Velocity * (int)direction, _rigidbody2D.velocity.y);
             _isRunning = false;
         }
     }

     public void Jump()
     {
         if (CanJump)
         {
             _rigidbody2D.AddForce(new Vector2(0f, JumpForce * _rigidbody2D.mass), ForceMode2D.Impulse);
         }
         else
         {
                OnCollidingOnCeiling(_entityTransform.renderer.bounds.max);
         }
     }

The game runs this code as usual, but it seems that changing the velocity or adding a force no longer works. Why is this happening? Has something drastically change in Unity 5 about 2D rigidbodies?

EDIT: A few things I forgot to mention:

  • The _rigidbody2D is referencing the parents rigidbody2D.

  • _rigidbody2D shows when the program gets to those lines, so it doesn't look like a problem of missing references.

  • I'm using UnityVS with Visual Studio Community Edition 2013, dunno if it matters.

Comment
Add comment · Show 2
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 sniper43 · Mar 04, 2015 at 09:47 AM 0
Share

You realize that if you append a script to an object that you don't need a _rigidbody2D reference, as you can directly call rigidbody2D (lowecase) to get in on the appended object? Same works if you want to reference the parents rigidbody2D with parent.rigidbody2D etc. This works for ALL items inherited from GameObjects.

Also, please put in the $$anonymous$$oveAllowed() and ChangeDirection() functions.

avatar image Mangas · Mar 05, 2015 at 11:47 AM 0
Share

I know I dont' need it, I do it to avoid Unity constantly calling GetComponent() for that rigidbody2d.

Actually the _rigidbody2D variable is referencing the parent rigidbody2D.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by ciwiaf · Mar 05, 2015 at 11:59 AM

this happened to my project too. Apparently, the "Apply Root Motion" in the Animation got enabled.. Try disabling that again.

Comment
Add comment · 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 Mangas · Mar 09, 2015 at 04:55 PM 1
Share

Solved!!! Thank you very much, now it's moving even though there are still some weird things that should not be happening...

avatar image
0

Answer by Goa · Mar 05, 2015 at 11:21 AM

You need to GetComponent in unity 5 Explained here: http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

Comment
Add comment · 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 Mangas · Mar 05, 2015 at 11:47 AM 0
Share

As a rule of thumb I always get the components I need in Start(), and make them private variables, so I'm already doing it:

 _rigidbody2D = GetComponentInParent<Rigidbody2D>();

The thing I forgot to mention is that the rigidbody2D is from the parent, but I don't think it matters anyway, the VS inspector shows the rigidbody of the parent at runtime.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

InvokeRepeating time doesn't sync within the same update function 2 Answers

Instantiated object not showing in scene or hierarchy 2 Answers

My balance in the game and my multiplier are not saving, i cant figure out why, any help? 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