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 kole_dole · Feb 04, 2021 at 11:51 PM · unity 5

Unity rb Player not sticking to a moving platform

I'm working on 2D Unity game. I want to make a moving platform on which player can stand, move, jump.

Player is dynamic Rigidbody2D with this settings

alt text

Platform is kinematic Rigidbody2D with this settings

alt text

For moving player I'm using velocity

 private void FixedUpdate()
 {
     rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
 }


For moving platform I'm using MovePosition

 private void FixedUpdate()
 {
 rb.MovePosition(transform.position + transform.right * Time.deltaTime);
 }

Player doesn't stick on platform, when platform gone player will fail down (because of gravity)

The Player must be dynamic Rigidbody2D, the platform does not have to be kinematic Rigidbody2D.

But when I remove physics from platform (also set player to be child of platform) player moves slower on the platform than on the ground and there is jittering that's why I made platform kinematic rb

Can someone tell me why player not moves with platform when both uses physics? And how to fix this

Thanks

1.png (15.3 kB)
2.png (12.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by RealFolk · Feb 05, 2021 at 12:55 AM

The player needs to be parented to the platform onCollisionEnter and unparented onCollisionExit. Respond if you need more help than that. This script goes on the player.

     private void OnCollisionEnter2D(Collision2D hitObject)
         {
             if (hitObject.gameObject.tag == ("FallingPlatforms"))
             {
                 transform.parent = hitObject.transform;
             }
         }
     
         private void OnCollisionExit2D(Collision2D collision)
         {
             if (collision.gameObject.tag == ("FallingPlatforms"))
             {
                 transform.parent = null;
             }
         }

Player rigidbody is dynamic. Moving platform is kinematic. Both SleepingMode is start Awake and CollisionDetection is Continuous. I just tried this and it works perfectly. @kole_dole

Comment
Add comment · Show 3 · 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 kole_dole · Feb 05, 2021 at 06:44 AM 0
Share

When I use parenting player doesn't move as expected ( is slower and there is jittering in movement) because of dynamic Rigidbody2D of the player. When Player is dynamic, Platform static, movement is as I expect, but player doesn't move together with platform (and then parenting doesn't work). Please help

avatar image RealFolk kole_dole · Feb 06, 2021 at 01:06 AM 0
Share

I move the platform with this:

 transform.position = new Vector2(transform.position.x + moveSpeed * Time.deltaTime, transform.position.y);
 
 
avatar image RealFolk kole_dole · Feb 06, 2021 at 01:07 AM 0
Share

I move the player with this:

 public int playerSpeed = 10;
     private bool facingRight = true;
     public float moveX;
     public float moveY;
     private Vector3 moveDirection;
 
     // Update is called once per frame
     void Update()
     {
 
         moveDirection = new Vector3(moveX, moveY).normalized;
     }
 
     void FixedUpdate()
     {
         Player$$anonymous$$ove();// calculates left/right movement at a fixed rate
     }
 
     void Player$$anonymous$$ove()// horizontal movement
     {
         moveX = Input.GetAxis("Horizontal");// sets moveX to left thumbstick
         moveY = 0;// 0 because we dont move up and down
 
         gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
     }
 
 
avatar image
0

Answer by Austin_A · Feb 05, 2021 at 07:01 AM

If parenting does not work try calculating the offset so in fixedupdate make a vector2 offset = new vector2( player,transform.position.x- platform.transform.position.x,0) That will give you the offset to do rb.MovePosition((player.transform.position + offset)*time.fixeddeltatime). I’m assuming the rigidbody is the players and that this script would be attatched to the play. On collision enter you would set a bool isOnPlatform to true and on collision exit set it false then only run the above code while isOnPlatform is true. @kole_dole

Comment
Add comment · Show 3 · 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 Austin_A · Feb 05, 2021 at 07:03 AM 0
Share

Sorry it should be platform.transform.position + offset in the moveposition params not player.transform.position

avatar image kole_dole · Feb 05, 2021 at 11:07 PM 0
Share

I'm using $$anonymous$$ovePosition for moving platform, rb.velocity for the Player. As I understand you sugget me to change position to player using $$anonymous$$ovePosition not rb.velocity right? Thanks

avatar image Austin_A kole_dole · Feb 06, 2021 at 04:28 AM 0
Share

Yes use move position for both. This way it will move the platform and the player at the same time in fixed update the same distance

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

202 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

Related Questions

Is updating from Gamemaker apk to Unity apk possible in the Play Store? 0 Answers

blend4web is better than unity WebGl ? 1 Answer

Unity 5 (All turns White) 0 Answers

How to disable close button of Editor Window? 0 Answers

GameObject.Find Unity 5.3+ 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