Learning Unity: March 11th – 14th 2024

I continue learning Unity.

I started by making the camera follow the player. To do this, we go into the Hierarchy and and drag the camera to the player, which will connect the two. However, this would just make the camera go inside of our player. While this would be fine with a first person game, it is not for our purposes. To fix this, we create a FollowPlayer script which we attach to the camera. The FollowPlayer script offsets the camera from the player to a specified distance with the offset variable. The final code for FollowPlayer should look like this:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

	public Transform player;
	public Vector3 offset;
	
	// Update is called once per frame
	void Update () {
		transform.position = player.position + offset;
	}
}

And that is all for the camera following the player.

Today, I will add collision between an obstacle and the player.

We start by creating a new script called PlayerCollision. We add code that outputs a string to the console as a test if the script is working. The code looks like this:

using UnityEngine;

public class PlayerCollision : MonoBehaviour {

	void OnCollisionEnter () 
	{
		Debug.Log("We hit something");
	}

}

This is where we encounter a major bug. I haven’t mentioned many of the bug I have had, but this one if very problematic. The OnCollisionEnter box just does not run. Another student working on Unity also cannot get the collision to work.

Leave a Reply

Your email address will not be published. Required fields are marked *