- Home /
2D moving platforms, physics issues
Hey all. I'm making a platform game with the Unity 2D tools. I have some moving platforms working, using Vector3.lerp. The problem here is when my character is on them. When the platforms move down, the character bounces up and down on it. When the platforms move up, the player does the same only less, it's more like he's vibrating.
From what I've looked into I'm thinking the issue is that the platforms move with
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
while the character moves using 2D physics forces.
Can someone confirm if that's the issue? And if it is, how would I go about changing my current platform script (below) to use forces?
Thanks.
void Start() {
startTime = Time.time;
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
void FixedUpdate() {
if (atStartMarker){
MovePlatformDown();
}
if (atEndMarker){
MovePlatformUp();
}
}
void MovePlatformDown(){
distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
platform.transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
if (platform.transform.position == endMarker.position){
//print ("Journey Down Complete");
atStartMarker = false;
atEndMarker = true;
distCovered = 0.0f;
fracJourney = 0.0f;
startTime = Time.time;
}
}
void MovePlatformUp(){
distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
platform.transform.position = Vector3.Lerp(endMarker.position, startMarker.position, fracJourney);
if (platform.transform.position == startMarker.position){
//print ("Journey Up Complete");
atStartMarker = true;
atEndMarker = false;
distCovered = 0.0f;
fracJourney = 0.0f;
startTime = Time.time;
}
}
The issue is most likely to be the colliders. The platform is moving up or down, when moving down you start falling faster than the platform and the colliders hit causing the jumping, on the way up, you're forcing the platform collider to merge with the player collider and it starts shaking.
The best way i can think of right now is not to use normal physics, ins$$anonymous$$d have a ray cast pointing down checking for the platform using a tag, if the platform is there, then use GetComponent to find out the direction its travelling in and then move your player up or down at the same velocity. It's a bit long winded but it has worked for me xD
Answer by Ferb · Jan 21, 2014 at 08:17 PM
I think you're probably right that the problem is caused by the platforms moving by having their positions changed in code, whereas the player is moved by physics forces. Admittedly, when I've had problems with that, it's been more often with things falling through each other rather than just shaking, but shaking wouldn't surprise me. These are the steps I think it'd take to change your moving platforms to use Unity's physics system.
Give each platform a rigidbody2D
Set them not to rotate, have gravitational scale of 0, have linear drag of 0
Give them a very high mass so the player won't move them by bumping into them
Set the rigidbody2D's velocity component manually
If you set the velocity to (endMarker.position - startMarker.position), that will start it going from the start point to the end point in 1 second, I think. Just multiply that vector by some float to change how long it takes. To test when to change the direction:
fracJourney = Vector3.Dot(endMarker.position - startMarker.position,platform.transform.position - startMarker.position) / (JourneyLength * JourneyLength);
Should give you 0 when the platform is at the startMarker, 1 when it's at the endMarker, and something outside that range if it gets past either of those points (which it probably will, just fractionally, enough that you'll have to test for fracJournery>1 rather than fracJourney==1)
Thanks for the reply, Ferb. I've done the above and it's definitely helped clean up the code (each platform function is now just one line), though I'm still having a similar visual problem; only now the character looks like he's vibrating whether it goes up for down. And when the platform reaches its highest point, the character hops up. I'm assu$$anonymous$$g this is all due to the forces being added to the character.
I've tried adding extra code to the player, something like Calum.$$anonymous$$c$$anonymous$$anus suggested above to check when he's on a platform, and then trying to equal out the forces, but having no luck. Any suggestions?
Your answer
Follow this Question
Related Questions
How would you make a pulling/sucking/attracting mechanic? 2 Answers
Using animated transform to push object away? 2D 0 Answers
Issues using physics for 2d platformer. 3 Answers
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers