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 DarKTower · Jun 08, 2014 at 11:26 PM · 2dmovementrigidbody2djumping

2D "Platformer" Movement Problems

Alright so I have movement working pretty much how I want too on the graphical side. The only problem I am having is getting jumping to work how I want too with the horizontal movement how I want too. Right now I am using a mix of RigidBody2D.AddForce() for jumping, and then transform.Translate() for horizontal movement as seen below. This is a multiplayer game so the Position of the player is being sent over the network, as well as the Velocity of the player.

So if the player is standing still and they jump (Space), they jump up and land perfectly fine, no problems. If the player is moving left or right (A/D, < / >), they move left or right perfectly fine, no problems. The problem is when the player jumps while moving left or right, the velocity of the player seems to drastically lower and they fall in "slow motion", barely lowering themselves. If you move in mid-air and then stop moving, the velocity kicks back in and you fall down like you should.

What I want is to allow the player to move while in mid-air, but to drop at the same time. There should be no "hanging" in mid-air while you are able to move left or right.

PlayerMovement.js

 private var moveSpeed: float = 7.5;
 private var jumpForce: float = 250.0;
 private var facingRight: boolean;
 
 function FixedUpdate() {
     if (networkView.isMine) {
         //JUMPING SCRIPT
         if (Input.GetAxis('Jump') && isGrounded()) { //IF GROUNDED
             rigidbody2D.AddForce(Vector2(0, jumpForce)); //JUMP THE CHARACTER USING FORCE
         }
     }
 }
 
 function Update() {
     if (networkView.isMine) {
         //HORIZONTAL MOVING CODE
         if (Input.GetAxis("Horizontal")) {
             transform.Translate(Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime, 0.0, 0.0);
             if (Input.GetAxis("Horizontal") >= 0.0) { //FACING RIGHT
                 thisHead.localPosition.x = 0.5;
                 facingRight = true;
             } else { //FACING LEFT
                 thisHead.localPosition.x = -0.5;
                 facingRight = false;
             }
         }
 }
 
 function isGrounded() {
     var result: boolean = Physics2D.Linecast(transform.position, groundChecker.position, 1 << LayerMask.NameToLayer("Ground"));
     if (result) {
         //rigidbody2D.velocity.y = 0;
         Debug.DrawLine(transform.position, groundChecker.position, Color.green, 0.5f, false);
     } else {
         Debug.DrawLine(transform.position, groundChecker.position, Color.red, 0.5f, false);
     }
     return result;
 }
 
 function OnSerializeNetworkView(stream: BitStream, info: NetworkMessageInfo) {
     if (stream.isWriting){
         //Executed on the owner of the networkview; in this case the Server
         //The server sends it's position over the network
  
         var pos: Vector3 = transform.position;
         var velocity: float = rigidbody2D.velocity[1]; //Only send the Y velocity, Networks can't send Vector2's
         stream.Serialize(pos);//Encode and send Position first
         stream.Serialize(velocity);//Encode and send Velocity second
     } else {
         //Executed on the others; in this case the Clients
         //The clients receive a position and set the object to it
         var posReceive : Vector3 = Vector3.zero;
          Debug.Log('Receiving '+posReceive);
         stream.Serialize(posReceive);//Decode Position first and save it
         transform.position = posReceive;
 
         var velocityReceive: float;
         stream.Serialize(velocityReceive);//Decode Velocity second and save it
         rigidbody2D.velocity = Vector2(0, velocityReceive);
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by oasisunknown · Jun 09, 2014 at 12:26 AM

If I am understanding right your issue is in how the physics works for unity.

your using a physics based update for the just (AddForce) but your over riding that with a non physics based movment when you move left or right by controlling the transform of the obj directly through transform.translate.

your best option is to do it all through the physics engine so there is no overlap.

let me link you to a video on the unity training archives that should help you clear this up.

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

the coding starts at around minute 38

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 DarKTower · Jun 09, 2014 at 12:29 AM 0
Share

Alright I decided to switch everything over to Physics-based, as I guess the problems I had with it were fixed in Unity 4.5. Before 4.5 there were tons of bugs with physics and 2D.

I also figured out that I only Serialize and Send the velocity of the Y, and just Serialize and Send the X of the transform. If I send both velocities it causes some weird problems on the client & server where they misread eachother.

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

Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer

Rigidbody2D AddForce only working with large numbers 2 Answers

adjust the player in the ground And movements 2 Answers

How do I make my character's movement less floaty? 1 Answer

How do I make a character Lunge? 2 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