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.MovePositionto 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;
}
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!
– GonFreaksI was wondering how you are getting the rigidbody2D.velocity if you are moving it by MovePosition. I´m asking this here, because I think it is directly related to your answer. Thank you very much and good work .
– Gabun88I'm also curious how you got that to work, since MovePosition does not return rigidbody2D.velocity... Also if you parent the player to the platform, while using MovePosition, the player does not move together with the parent platform. I'm confused.
– snifoWe 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/
– spacelizardstudio