- Home /
Transform.position: Moving Platform Support Logic
I'm trying to understand the code for the moving platform support in the Unity side-scroller tutorial.
The relevant code is as follows:
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
activeGlobalPlatformPoint = transform.position
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
transform.position = transform.position + moveDistance;
As I understand it the reason this code exists is because the character controller will not automatically react to a moving collider.
So basically we are subtracted the player's position form the active platform's position in World Space. My understanding is that this should move the character along with the platform.
If the platform is at say (0,1,0) and the player is standing on top of it at (0,2,0) then move distance should equal (0,-1,0) correct? This would move the player's position downward just by standing on the platform.
Here is where I get lost in the logic:
If the platform moves up to (0,2,0) and the player is at (0,2,0) then moveDistance = 0. The purpose of the code as I understand it is to move the player, so his World Space coordinate wouldn't automatically move, and thus be (0,2,0) at this point.
My question is simply this: How does this move the player relative to the moving platform?
Answer by Loius · Oct 03, 2010 at 04:00 PM
Egad those variable names they are so long.
Alright, going into the function we have:
transform.position = (0,2,0)
activePlatform.position = (0,1,0)
then from those we know that
activeLocalPlatformPoint = (0,1,0)
activeGlobalPlatformPoint = (0,2,0) // I think. :p
Then we move the platform to
activePlatform.position = (0,2,0)
and run the code:
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
// ((0,2,0)(no rotation)).TransformPoint( (0,1,0) ) = (0,3,0)
// basically point addition when there's no rotation happening.
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
// alpp = (0,2,0).ITP( (0,2,0) ) = (0,0,0)
// finding the relative position of player to platform
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
// (0,3,0) - (0,2,0) = (0,1,0)
transform.position = transform.position + moveDistance;
// moveDistance = (0,1,0)
You have activeLPP equaling (0,0,0) when it is set, but equaling (0,1,0) when newGPP is set. When did it pick up the extra y coordinate?
Your answer
Follow this Question
Related Questions
Change 2D box collider Y values but how offset stay on same position and adjust their values. 0 Answers
How to add knockback when player hit enemy 2D Platformer 0 Answers
Sprite turning around repeatedly against a wall. How to have "Wallslide" flip sprite? 0 Answers
Developing a 2D level Editor for a 2D game 2 Answers
2d Platform tutorial problem 2 Answers