Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by alexkaskasoli · Apr 09, 2014 at 11:01 PM · rigidbodycharactercontrollerplatformer

Simple Rigidbody2D controller (Platformer Jump)

I'm trying to make a very simple character controller script for an object with a rigidbody2D (aiming for basic 2D platformer gamelay with WASD and Space to jump).

 void HandleMovement() {

     if (Input.GetKey (KeyCode.D)) {
         rigidbody2D.velocity = new Vector2(walkForce, rigidbody2D.velocity.y);
         print(rigidbody2D.velocity);
     }
     else if (Input.GetKey (KeyCode.A)) {
         rigidbody2D.velocity = new Vector2(-walkForce, rigidbody2D.velocity.y);
     }


     if (Input.GetKeyDown(KeyCode.Space) & grounded) {
         jumping = true;
         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
     }
 
 }

I can't understand why my HandleMovement method doesn't work. In my case no matter how big a value I put for walkForce or jumpForce, the object doesn't move a bit. The velocity is set correctly because it prints out the correct velocity when D is held, it's just that the object doesn't show any sign of moving. What could be causing this?

EDIT: I've solved this by runing the HandleMovement in FixedUpdate instead of Update. The controls now work but the jump is very "buggy-ish". How can get a nice fluid platformer jump with a rigidbody2d? What I've done above just doesn't play nicely.

Comment
Add comment · Show 5
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 ToxxicSin · Apr 10, 2014 at 12:36 AM 0
Share

Try using something like

 void Handle$$anonymous$$ovement() {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) & grounded) {
        jumping = true;
        rigidbody2D.AddForce(0f, jumpForce);
     }
 }

Ins$$anonymous$$d of applying a velocity, you can just add an upward force to the object. The x value doesn't require a force because it will retain whatever velocity the object already has. Don't forget to still set your jumping value to true. And also be sure to set it back to false when object touches the ground again if you are working on a platformer of sorts.

avatar image alexkaskasoli ToxxicSin · Apr 10, 2014 at 06:02 AM 0
Share

Thanks, I tried that and it works but I still find the jump not very smooth. And also for some reason the character doesn't always jump to the same height

avatar image ToxxicSin ToxxicSin · Apr 10, 2014 at 10:30 PM 0
Share

Do you have any other scripts? Anything outside that could be effecting it. $$anonymous$$aybe your character has more than one collider touching itself

avatar image ToxxicSin ToxxicSin · Apr 10, 2014 at 11:16 PM 0
Share

the current script you already has works fine for me. I would check to make sure you don't have something else effecting it

avatar image ToxxicSin · Apr 10, 2014 at 11:20 PM 0
Share

@alexkaskasoli

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Moving platform clips through character controller 2 Answers

Detecting the right time to play a landing animation? 2 Answers

How to handle movement in a multiplayer FPS? 0 Answers

character controller + physics 1 Answer

Sphere + rigidbody + character controller 1 Answer


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