Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving camera with the mouse left and right

Discussion in '2D' started by YoYoYoYo_111_PiPi, Dec 8, 2016.

  1. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Hi, is it possible, if our canvas or our background is bigger than our camera view (it surpasses it horizontally), to place our mouse on the right or left side of our screen and make the camera move into that direction? I would like that the movement happens only in certain areas near the edge, and not to react in the middle, for instance. Also, would like to be able to control the speed of the movement. Is this possilbe in Unity, and if so, how would you go about doing something like that?
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    Yes, you'd do this with Camera.ScreenToViewportPoint and Input.mousePosition.

    Code (csharp):
    1. Vector3 viewportPos = Camera.main.ScreenToViewportPoint(Input.mousePosition;
    Then just check viewportPos.x, it should now be a number between 0 and 1. You can then move left if it's smaller than for example 0.1 and right if its bigger than 0.9.
     
    YoYoYoYo_111_PiPi likes this.
  3. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Thanx for the info, will write here if there are some problems.