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;
		}
	}

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)