Week 7

Work week!

Although ... you should totally know how joysticks work.
Oh, and auto-format in monoDevelope is control-i.
paltform and player

Moving Platforms

There's lots of ways to make a moving platform. You can lerp them like we did in week 5. Here's a handy way to do it in two lines using a sine wave.
Code goes on platform
	{
	private Rigidbody rb;
	public bool go = false;
	public float speedY = 0.5F;
	public float speedX = 0.5F;
	public float speedZ = 0.5F;
	void Start ()
	{
		rb = GetComponent< Rigidbody > (); 
	}

	void FixedUpdate ()
	{
		if (go) {
			Vector3 Step = new Vector3 (Mathf.Sin (Time.time) * speedX, Mathf.Sin (Time.time) * speedY, Mathf.Sin (Time.time) * speedZ);
			rb.MovePosition (rb.position + Step);
		}
	}
 
We're using Time.time as an iterating variable. If you want the platform to go twice as far you could divide Time.time by 2.

Sticking your character to the platform

Yeah, this is trickier than it should be. There are lots of solutions that say you should make the character a child of the platform, but the results I was getting were pretty spotty at best. Here's how I thought through a solution. You'll want to add a capsule trigger collider to your FPSController.
If you're using a rigidbody, make the new trigger collider slightly larger than the other collider. Also in the rigidbody turn on "interpolate" - this makes it smother.
Make sure the platform has a tag called "platform" assigned to it
Code goes on Player
	public bool onPlatform = false; 
	private Vector3 platformStartLocation;
	public Transform platTransform; 
	private Rigidbody rb; 

	void Start () {
		rb = GetComponent< Rigidbody >(); 
	}
		
	void FixedUpdate () {
		if (onPlatform){
			// get the difference between the current location of the platform and the last location 
			Vector3 offset = platTransform.position - platformStartLocation; 
			// add the difference in the platform location to the location of the player 
			// rb.MovePosition(transform.position + offset); // if the player has an rb you could move it this way
			transform.position += offset; 
			// set the start position to the current position
			platformStartLocation = platTransform.position; 
		}
	}
	//void OnCollisionEnter (Collision col) // you could use collision instead if the player has a rigidbody
	void OnTriggerEnter (Collider col)
	{
		if (col.gameObject.tag == "platform") {
			onPlatform = true; 
			platformStartLocation = col.transform.position; 
			platTransform = col.transform; 
		}
	}

	//void OnCollisionExit (Collision col)
	void OnTriggerExit (Collider col)
	{
		if (col.gameObject.tag == "platform") {
			onPlatform = false; 
		}
	}