Search
  • Charles Low

Further Challenges to the Basic Vehicle Movement Tutorial

Updated: Oct 26


Additional Challenges:

  1. Stack crate obstacles;

  2. Add oncoming traffic;

  3. Create a second camera view; and

  4. Create split-screen multiplayer function.


SCRIPT EXTRACT FOR SECOND CAMERA VIEW

====================================


Create a new camera object - remember to remove audio listener for the new camera object.

    // Variables for cameras
    public Camera mainCamera;
    public Camera secondCamera;

   <...>

    // Update is called once per frame
    void Update()
    {

    <...>

        // This is for the activation and deactivation of cameras. First we create an input key to deactivate the active camera and activate the inactive one. We choose to use "C" as a key (any key will do).

        if (Input.GetKeyDown(KeyCode.C))

        {
//What we are saying here is that when "C" is pressed. the mainCamera in its enabled state will become disabled. Likewise, the second camera will be disabled if it is enabled already.
            mainCamera.enabled = !mainCamera.enabled;
            secondCamera.enabled = !secondCamera.enabled;
        
        }
    }

For some reason the highlighted part results in a disabled camera becoming activated as well (and thus the player is able to switch cameras) - though for now it is not clear why pressing C activates a disabled camera.


SCRIPT EXTRACT FOR MULTIPLAYER SPLIT-SCREEN FUNCTION

=====================================================


First create a public variable for InputID:

    public string InputID;

Then inside the Update method, add InputID to the way inputs are made to the player's movements:


        horizontalInput = Input.GetAxis("Horizontal" + inputID);
        forwardInput = Input.GetAxis("Vertical" + InputID);

Then go to the Input Manager and create a new Input Array for the horizontal and vertical movements.

Then duplicate the player object and camera objects.

Adjust the viewport rect - for the second player cameras, adjust the viewport rect to 0.5 on X and W.


Assign the player objects and camera objects from the Hierarchy to the Inspector accordingly.

Assign InputID of 1 to the first player and InputID of 2 to the second player in the Inspector.



For practice sake I repeated this exercise with different objects.


10 views0 comments

Recent Posts

See All