logo


Home > Game / Unity C# Script > Unity C# Script - Camera Controller

Unity C# Script - Camera Controller (Introduction, Inspiration, How to generate a script, CameraController.cs and How to use the script)

Introduction

You can drag the mouse to rotate the camera so as to look around the scene by using this script.

This script can be used in a game or an animation.

Note: If you wish to record the cursor movement with the Unity Recorder, you would need another script: Unity C# Script - Arrow Cursor Renderer

Go to Top

Inspiration

YouTube video : Take a 360° VR Tour of the Krusty Krab! 🍔 | SpongeBob

Play the video on a computer browser. You can drag the mouse to move the camera.

Play the video on a YouTube app on a phone. You can move/rotate your phone, or swipe, and the camera will move!

Go to Top

How to generate a script

The script on this webpage was generated with Microsoft Copilot ( https://copilot.microsoft.com ). Copy and paste this script to Microsoft Copilot if you wish to understand this script.

Prompt: "i want a c# script for 360 camera support for webgl build on mobile phone browser, i want to use the gyroscope of mobile phone to rotate the camera of unity. however, on computer browser, i want to use mouse down and drag to rotate the camera. on mobile phone browser, i want to use also swipe to rotate the camera."

Go to Top

CameraController.cs


using UnityEngine;

public class CameraController : MonoBehaviour
{
    private Vector2 touchStart;
    private bool isMobile;
    private bool isGyroEnabled;
    
    private float rotationSpeed = 1f;
    
    void Start()
    {
        isMobile = Application.isMobilePlatform;
        isGyroEnabled = EnableGyro();
    }
    
    void Update()
    {
        if (isMobile)
        {
            if (isGyroEnabled)
            {
                GyroRotation();
            }
            else
            {
                TouchRotation();
            }
        }
        else
        {
            MouseRotation();
        }
    }
    
    // Gyroscope-based rotation
    private void GyroRotation()
    {
        Quaternion gyroRotation = Input.gyro.attitude;
        transform.rotation = Quaternion.Euler(90f, 0f, 0f) * new Quaternion(-gyroRotation.x, -gyroRotation.y, gyroRotation.z, gyroRotation.w);
    }
    
    // Swipe-based rotation
    private void TouchRotation()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                touchStart = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Vector2 direction = touch.deltaPosition;
                transform.Rotate(Vector3.up, -direction.x * rotationSpeed * Time.deltaTime);
                transform.Rotate(Vector3.right, direction.y * rotationSpeed * Time.deltaTime);
            }
        }
    }
    
    // Mouse drag rotation
    private void MouseRotation()
    {
        if (Input.GetMouseButton(0))
        {
            float x = Input.GetAxis("Mouse X") * rotationSpeed;
            float y = Input.GetAxis("Mouse Y") * rotationSpeed;
            
            transform.Rotate(Vector3.up, -x);
            transform.Rotate(Vector3.right, y);
        }
    }
    
    // Enable Gyroscope
    private bool EnableGyro()
    {
        if (SystemInfo.supportsGyroscope)
        {
            Input.gyro.enabled = true;
            return true;
        }
        return false;
    }
}

Go to Top

How to use the script

1. Create a new Unity project which uses Universal 3D.

2. Project window > Assets > Right click > Create > C# Script > Name the script "CameraController.cs"

3. Click on the file "CameraController.cs" > Open

4. Copy the script on this webpage. In the opened file "CameraController.cs", paste the script and save the file.

5. From the Project window, drag the script "CameraController.cs" to the Main Camera in the Hierachy window.

6. (Optional) You can add some 3D game objects to the scene.

7. Click the Play button.

8. In the Play Mode, drag the mouse in the Game window. You can see that the camera moves when the mouse is being dragged!

9. Click the Play button again to exit the Play Mode.

10. File > Build Settings... > Platform > WebGL > Switch Platform >

11. Player Settings... > Publishing Settings > Select "Decompression Fallback" > Close the "Project Settings" window

12. In the "Build Settings" window > Click "Build and Run"

13. The WebGL build will appear in a browser window. Drag the mouse in the browser. You can see that the camera moves when the mouse is being dragged!

14. (Optional) Try to see if the WebGL build works on a phone browser.

Go to Top




YouTube (@nlartwork)

  • Copyright (C) 2025 NL Artwork | All Rights Reserved.
  • Go to Top