Game Scene
Main Camera
The “Main Camera” object has a FollowCamera on it.
public class FollowCamera : MonoBehaviour
{
public Transform m_target;
Vector3 m_offset;
Vector3 m_original;
// Start is called before the first frame update
void Start()
{
if (null == m_target)
{
m_target = FindFirstObjectByType<PlayerBird>().transform;
}
m_original = transform.position;
m_offset = m_original - m_target.position;
}
// Update is called once per frame
void LateUpdate()
{
if (null != m_target)
{
Vector3 pos = m_target.position + m_offset;
pos.y = m_original.y;
pos.z = m_original.z;
transform.position = pos;
}
}
}
This script just makes the camera follow along with its m_target (the player), but only along the x axis.
I’ve used the LateUpdate() function to make sure the player moves first and then the camera follows after.
Penguin
The “Penguin” object is a prefab with an animated sprite on it.
It has a PlayerBird script attached to it already.
Sky
“Sky” has a RepeatBackground on it. We’ll be doing some work in that script soon.
“Sky” has a single child object “Image” with a sprite on it.
GameManager
We’ll be managing the flow of the game through the GameManager script, and one is already attached here.
You may notice that there’s a pause menu already set up, and that is managed through the GameManager.
PlayerBird.cs
public class PlayerBird : Bird
{
public float m_forwardSpeed = 3.0f;
// Update is called once per frame
protected override void Update()
{
m_flap = Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space);
m_glide = m_flap || Input.GetMouseButton(0) || Input.GetKey(KeyCode.Space);
{ // TODO Move the player bird forward at a rate of m_forwardSpeed (units per second)
}
base.Update();
}
}
PlayerBird doesn’t do much other than inherit from Bird.
When I set that up, I imagined that you might want to re-use the bird behavior to create enemy birds to avoid or something like that.