C# script to make camera move, when mouse cursor is near sides of the screen.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net

public class CameraController : MonoBehaviour {
    public float offset;
    public float speed;

    //x - min y - max
    public Vector2 minMaxXPosition;
    public Vector2 minMaxYPosition;

    private float screenWidth;
    private float screenHeight;
    private Vector3 cameraMove;

    // Use this for initialization
    void Start()
    {
        screenWidth = Screen.width;
        screenHeight = Screen.height;

        cameraMove.x = transform.position.x;
        cameraMove.y = transform.position.y;
        cameraMove.z = transform.position.z;
    }

    // Update is called once per frame
    void Update()
    {
        //Move camera
        if ((Input.mousePosition.x > screenWidth - offset) && transform.position.x < minMaxXPosition.y)
        {
            cameraMove.x += MoveSpeed();
        }

        if ((Input.mousePosition.x < offset) && transform.position.x > minMaxXPosition.x)
        {
            cameraMove.x -= MoveSpeed();
        }

        if ((Input.mousePosition.y > screenHeight - offset) && transform.position.y < minMaxYPosition.y)
        {
            cameraMove.y += MoveSpeed();
        }

        if ((Input.mousePosition.y < offset) && transform.position.y > minMaxYPosition.x)
        {
            cameraMove.y -= MoveSpeed();
        }

        transform.position = cameraMove;
    }

    float MoveSpeed()
    {
        return speed * Time.deltaTime;
    }
}

 

Unity C# 2D Camera Mouse Controller
Tagged on:         

2 thoughts on “Unity C# 2D Camera Mouse Controller

  • July 16, 2017 at 3:52 pm
    Permalink

    This place is awesome.
    The resources that could be found here are so damn helpful.

    Thank you for sharing all this stuff!

    Reply
    • July 16, 2017 at 7:32 pm
      Permalink

      I’m glad that those scripts are helpful, have fun with them 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *