# A good approach on working on player controller for movable platforms?

I have got my player controlling working out decent enough, except with mediocre animation controller for the player, but oh well. All I have done so far is basic input: move, run, jump, multi-jump. How would I approach making me player conform to the movement of the moving platform. I know to move the platform relative to my player on top of adding more force if player has moved. I have looked up some resources on line, but I am confused why they use certain methods.

``````def Platform ():
//BROKEN
if (activePlatform != null):
newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint)
moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint)

if (moveDistance != Vector3.zero):
controller.Move(moveDistance)

lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime

else:
lastPlatformVelocity = Vector3.zero

activePlatform = null

if activePlatform != null:
activeGlobalPlatformPoint = transform.position
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position)
``````

I don't understand why it is important to get local and world positions. Also, I don't understand moveDistance calculation.

My first intuition was doing something like moveDistance.y or moveDistance.x and changing them depending on the hit object positive or negative velocity. Just getting some ideas.

I also use:

``````def OnControllerColliderHit (hit as ControllerColliderHit):
if (hit.moveDirection.y < -0.9) & (hit.normal.y > 0.5):
activePlatform = hit.collider.transform
``````

to check which platform I have collided with and strictly focusing on the one below the player.

-
What is the pros and cons of parenting to platform versus apply relative force based on platform? – zyeek Jan 19 '13 at 19:57

## 1 Answer

Well, one advantage of parenting your player to the moving platform versus applying constant force is that (at least with Unity) the former is easier to implement. When you want your player GameObejct to behave as if it's on some moving platform, just set its (the player GameObject's) parent to "platform" and then you're good to go. Obviously in this situation, you will always use the player local transforms to move him around.

==UPDATED as per comment==

Ok, don't worry, I've been frustrated by this as well before finally getting it. You need to familiarize yourself with some basic Unity notions:

• what does it mean when you're moving (translating) a GameObject via its global coordinates (which are the same for him and any other GameObject in the scene) vs. moving it via its local coordinates:

Make a Cube. Rotate it a little in all directions. Then make a script in which you make him move slowly in one direction. Switch that translation from local to global. Look at how it changes its movement, something like:

``````//this goes in the Update function of the script you attach to the cube:
int x = x+10; //adjust the +10 if it moves too fast
Vector3 translation = new Vector3(x,0,0);

//comment out one of the above lines to see local vs global translation:
transform.position += translation; //global translation on the world's x axis
transform.localPosition += translation; //translation on the objects x axis (which, if you rotated it a little after creation, will not be the same as the world's x axis
``````

Also check out the documentation for the Transform class here:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

it explains pretty well what you can do with it and how it works.

• How do local and global translations get influenced by the fact that the cube has a parent which is also moving.

Create a large plane. Make the Cube the child of the plane, either by visually dragging it on to the plane in the inspector, or by adding the following code in the Cube's script's Start function:

``````public GameObject plane//drag the plane object here in the Inspector

//inside the Start function:
transform.parent = plane.transform;
``````

Now hit play, and for each of the local and global movenents of the Cube, also move (by hand, in the viewport) the plane in various directions. Notice how the Cube global and local translations are influenced by this (or, as the case may be, are NOT at all influenced by this).

• Once you get this, make a simple controller for the cube. Something real basic, like in should translate the Cube forward on its (local) x-axis when you press "W". This should replace the +10 automatic translation we were doing in the original script. Re-do the above examples and see how that works.

Finally, one great newbie tutorial you should watch is Lynda's Unity Essentials, it helped me A LOT in understanding the basics of Unity. And it actually has dedicated chapters on how translations work and on how to make your own 1rst and 3rd person controllers.

-
I have tired this and for the life of me I keep getting confused in using Unity's functions to work with local location and world location to get the correct movement. For example, the model I currently have has it's pivot different from global. Moving forward with respect to the appear of the model would be moving forward require change in the z axis (local), whereas globally it is x axis movement. Any enlightenment would help. SO far, I just took Unity's already provided 2D platform controller and modified it. I would really like to understand it as i hate black box scenarios. – zyeek Jan 22 '13 at 4:40