Tag Archives: follow

Making the camera follow the player in a 2D Unity game with code

This tutorial will show you how to write a script in C# code that will make the camera follow the player in a 2D Unity game. Watch the video below and then scroll down for the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

  public GameObject player;
  public float offset;
  private Vector3 playerPosition;
  public float offsetSmoothing;

  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
    playerPosition = new Vector3 (player.transform.position.x, transform.position.y, transform.position.z);
    if (player.transform.localScale.x > 0f) {
      playerPosition = new Vector3 (playerPosition.x + offset, playerPosition.y, playerPosition.z);
    }
    else {
      playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    }

    transform.position = Vector3.Lerp (transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
  }
}

Next tutorial: Stopping the player from sticking to the edge of platforms and walls

Extruding along a curve

This video explains how to extrude a circle along a curve and then animate an extrusion. When we ‘extrude’ a 2D shape such as a circle, it means that we will push that shape out. By using a curved line, we can push the shape out so that it follows the line.

An extruded circle along a curve
An extruded circle along a curve

Watch the video below to see how to do this in Maya.

Tip: When selecting the circle and CV curve, select the circle first and then hold down Shift key to select the CV curve. Make sure you select the curve last.

How to orient a symbol to a motion tween path in Adobe Flash CS6

This video tutorial explains how to orient a symbol to the path when creating a motion tween effect in Adobe Flash CS6. You can watch the video on YouTube by clicking here or view it below.