Fixing the camera.
To play this game, we want to look at the action from the top down. To do this, we need to change the angle of the camera and its viewing angle. So select a camera in the Hierarchy and you will see a small window appear in your scene showing a preview of what it sees.
This also opens up some details in the "Inspector" on the right, where it says “Rotation”, we will change the X axis to "90".
Now move the camera up and away from the scene until you see the entire map.
But we still need a way to control our game. To do this, we will need to write a script.
Create a new folder in your resources and name it "Script". Now right click anywhere here and select:
Create > C# Script
Name your new script "TiltControl".
Once this is created, double-click on it to open the default editor (IDE). This will usually be Visual Studio.
Now just delete everything that is there at the moment and replace it with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TiltControl : MonoBehaviour
{
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
Vector3 movement = new Vector3(Input.acceleration.x, 0.0F, Input.acceleration.z);
rb.velocity = movement * 5;
}
}
Here we call the solid body component that we added earlier, and then add the velocity along three axes based on the accelerometer in the phone. In other words, the player will now be able to move the ball by tilting the phone.
Now go back to Unity, select the sphere and drag your TiltControl script to the inspector at the bottom where it says "Add Component". Now this means that the code in your script will affect the game object to which you have attached it.
Before we test the game, you should also check the box labeled "Freeze Position Y" under the restrictions. This is important because it will prevent our ball from jumping out of the maze if it moves too fast!