Tuesday, 9 April 2019

Setup GIT with your Unity Projects: first ignore machine specific content

GIT is a great platform for archiving, reviewing, versioning and sharing source code. I use especially  Bitbucket by Atlassian, it's sleek with a good amount of functionality and great pricing.

When creating a GIT repository for Unity the first thing I do is to immediately a .gitignore file. This allows versioning only what's really relevant. Especially exclude storage hungry directories like "Build" or "Library" and avoid creating conflicts and noise when deploying or migrating to a new laptop or platform.

This also means that whenever a change is pulled from the repository, Unity will rebuild the machine-specific directories and files for that machine. Hence the settings of Unity UI will remain specific for each configuration. See an example below

Assets/TextMesh Pro/
# Visual Studio cache directory.vs/
# Autogenerated VS/MD/Consulo solution and project filesExportedObj/
# Unity3D generated meta files*.pidb.meta*.pdb.meta
# Unity3D Generated File On Crash Reportssysinfo.txt
# Builds*.apk*.unitypackageBuilds-*.symbols.zip

Monday, 8 April 2019

My first challenge in mobile game development: how to scroll the screen in a 2D scene?

I'm Guillaume, and amongst other things, I'm self-learning game developer at Sofielafée. A homemade art studio creating illustrations and games for toddlers and pre-schoolers.

I'm going to write about my journey in game development and especially the technical aspects of this blog. I don't intend to share the whole project since it does not bring much value. I faced many challenges and the idea is to share hints and tips along the way.

When I had this iPad learning game in mind I've naturally started to read and experiment on Apple's native platform using Swift 5 and XCode. The Swift playground was okay for a bit but I was after some more adventurous materials.

After some reading and a bit of research, I ended up choosing Unity. Mostly for its community, a great number of learning materials and its versatility.

The Unity beginners tutorials are great and I was especially interested in the space shooter tutorial. This scene and game mechanics were close to what I was looking to build for Rocket Mouse. I want a rocket flying on the screen with a scrolling camera to emphasize on ethereal outer space movements.

So my first C# Unity script is a monobehaviour to be attached to the main camera. With this script I wanted the camera to follow the player on the horizontal axis with a smooth chase. When the player reaches the border of the screen the camera starts to catch him up.

See code in action in Rocket Mouse on iPhone

And the script in C#

public class CameraController : MonoBehaviour // a monobehaviour script to attach to the main camera

    public float dragSpeed = 1.0f// camera is dragging behind the player for a smoother movement
    public float cameraZPosition = -10.0f// camera depth
    public float xBoundary = 2f// horizontal boundary where the camera movement is triggered
    public bool followPlayer = true// to follow or not to follow

    private GameObject playerObject; // reference to the player object

    private Vector3 targetDirection; // target direction for the camera 

    // Use this for initialization
    void Start()
        playerObject = GameObject.FindWithTag("Player"); // find the player
        if (playerObject == null)
            Debug.LogWarning("No Player Tag found");
        transform.position.Set(transform.position.x, transform.position.y, cameraZPosition); // initialise camera postion

    // Update is called once per frame
    void LateUpdate()
        if (followPlayer && playerObject != null// is there a player to follow
            targetDirection = new Vector3(playerObject.transform.position.x, transform.position.y, cameraZPosition); // calculate the new target for the camera
            targetDirection = Vector3.zero; // else no target

        transform.position = Vector3.Slerp(gameObject.transform.position, targetDirection, Time.deltaTime * dragSpeed); // Spherical Interpolation to smooth the movement
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -xBoundary, xBoundary), transform.position.y, cameraZPosition); // set Z axis to default