- Home /
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 pointsI 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;
}
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!
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 .
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.
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/
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
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!
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.