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
1
Question by rastating · Jun 27, 2014 at 10:19 PM · 2drigidbody2dmoving-platform

Moving Rigidbodies (2D) with moving platforms

I've been searching for days to try and resolve the issue I'm having but have failed at every turn and haven't found the solutions that have been worked for other people to work for me :( Below is a summary of what I am trying to achieve:

  • I have some platforms that are using rigidbody2D.MovePosition to move back and fourth between two points

  • I have a user controlled character that is being moved by setting the velocity of the attached rigidbody2D manually

  • When a character is on top of a platform, I want the platform to move the player with it, as to keep it on the platform while it moves rather than slipping out from underneath it

  • When a character is on a moving platform, I want to be able to walk on the platform, i.e. not keep the player in a locked position on the platform

One of the solutions I've tried, which almost worked, was making the parent transform of the character be the moving platform; this caused issues however when trying to move on the platform (see fourth bullet point).

If the character moves in the direction against what the platform is moving in (e.g. if the platform was moving left and the character was trying to walk to the right on the platform) then the two forces would begin to cancel each other out and the character would appear to move very slowly, rather than at a speed as if walking on static ground.

I've tried experimenting with physics materials, but not had any luck without hitting side effects such as when trying the parenting solution.

All suggestions on how to overcome this are welcome

Thanks :)

Update

As per the accepted answer, I changed my code to add the velocity of the moving platform to the character's velocity after I have set the character's velocity (emphasis on after setting the character's velocity otherwise odd effects will occur) and it worked perfectly.

Below is a code sample of what I have done, the activePlatform variable is a game object that represents the platform my player is currently standing on.

 // Set movement velocity and the animation state.
 float axisFactor = Input.GetAxis ("Horizontal");
 this.rigidbody2D.velocity = new Vector2 (axisFactor * this.maxSpeed, this.rigidbody2D.velocity.y);
 
 if (activePlatform != null) {
     this.rigidbody2D.velocity += activePlatform.rigidbody2D.velocity;
 }

Comment
Add comment · Show 4
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 GonFreaks · Nov 20, 2014 at 07:35 PM 0
Share

I'm trying the same thing right now! Would you like to share your code? I've been at this for 5 days straight because it's so much fun learning to code and playing with physics. I'm still struggling with the structure of my project, and what is a parent/child etc. It's all great fun! Cheers!

avatar image Gabun88 · Jan 24, 2015 at 12:13 PM 0
Share

I was wondering how you are getting the rigidbody2D.velocity if you are moving it by $$anonymous$$ovePosition. I´m asking this here, because I think it is directly related to your answer. Thank you very much and good work .

avatar image snifo · Feb 27, 2015 at 08:53 PM 0
Share

I'm also curious how you got that to work, since $$anonymous$$ovePosition does not return rigidbody2D.velocity... Also if you parent the player to the platform, while using $$anonymous$$ovePosition, the player does not move together with the parent platform. I'm confused.

avatar image spacelizardstudio · Mar 03, 2016 at 01:54 PM 0
Share

We solved the problem by using a SliderJoint2d and setting the gravity scale of the character to zero while on the moving platform. Here is the link with the full explanation:

http://spacelizardstudio.com/devblog/index.php/2016/03/02/unity-a-guide-to-moving-platforms/

2 Replies

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

Answer by Omberone · Jun 27, 2014 at 11:56 PM

Your solution should be to add your wanted velocity with the platforms velocity

 myVelocity = 2x, 0y  
 platformVelocity = -3x, 0y  
 myVelocity += platformVelocity  
 myVelocity = -1x, 0y  

Comment
Add comment · 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 rastating · Jun 28, 2014 at 12:50 AM 0
Share

Thank you! I'm not sure how I didn't land on this logic previously, but I just implemented it and it did the job perfectly :D I'll edit my question to include a code sample of what I did for anyone else having this issue. Thanks again!

avatar image Omberone · Jun 28, 2014 at 12:12 PM 1
Share

Glad I could help :)

avatar image
1

Answer by ErikH2000 · Jun 27, 2014 at 11:55 PM

You could call AddForce() on the character's rigidbody while it is on the moving platform. You would AddForce with a vector in the same direction as the moving platform is travelling.

For this to work with point 4, I think you need to also use AddForce() instead of SetVelocity() for moving your character. Otherwise, SetVelocity() may override the AddForce() of the moving platform. You want to the forces of character control and platform movement to be combined together which is why you would not call SetVelocity() for the character.

I'm sure there are other ways to do it too, but this is what I would try first.

Comment
Add comment · 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

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

27 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

Related Questions

Using child colliders with rigidbodies/joints in 2D 0 Answers

Drag and throw Rigidbody2d (Angry Birds style) 0 Answers

2D character movement is jittery (top-down) 1 Answer

Create a simple constantly swinging vine/rope 1 Answer

Turn based 2d star ship movement 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