Unity Junior Programmer Mission: Create With code Challenge 1 – Program an Airplane to fly

I was challenged to use the skills learned in the driving simulation to fly a plane around obstacles in the sky. I was required to get the user’s input from the up and down arrows in order to control the plane’s pitch up and down. I also needed to make the camera follow alongside the plane in order to keep it in view.

https://meta.dwdenney.com/challenge1/

Challenge Outcome:

  • The plane moves forward at a constant rate
  • The up/down arrows tilt the nose of the plane up and down
  • The camera follows along beside the plane as it flies
C# Code Sample
public class PlayerControllerX : MonoBehaviour
{
    public float speed;
    public float rotationSpeed;
    public float verticalInput;
    public float horizontalInput;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // get the user's vertical input
        verticalInput = Input.GetAxis("Vertical");
        horizontalInput = Input.GetAxis("Horizontal");

        // move the plane forward at a constant rate
        transform.Translate(Vector3.forward * speed * Time.deltaTime * verticalInput);

        // tilt the plane up/down based on up/down arrow keys
        transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime * horizontalInput);
    }
}

public class SpinPropX : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(0, 0, 5);
    }
}

public class FollowPlayerX : MonoBehaviour
{
    public GameObject plane;
    private Vector3 offset = new Vector3(30.8f, 1.4f, 0.2f);

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.position = plane.transform.position + offset;
    }
}


Leave a Reply