Farflame
Farflame

Reputation: 421

Unity 2D Mouse Zoom, but zoom in to location of mouse point

Edit : I need an answer for a 2D implementation, not 3D. So the 'Asked Before' question doesn't work for my project.

Edit : Using the accepted answer below, this is my working script. It's slightly off because the 0.1F part of the Vector3 is hard to get right, but it works. Time.deltaTime didn't work though.

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

public class CameraZoom : MonoBehaviour
{

private float zoom = 10;
Vector3 newPosition;

void Update()
{
    if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
    {
        zoom -= 1;
        Camera.main.orthographicSize = zoom;
        newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = Vector3.Lerp(transform.position, newPosition, 0.1F);
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
    {
        zoom += 1;
        Camera.main.orthographicSize = zoom;
    }

}

}

Original Question : I have a basic script for zooming in and out, but when I zoom back in with the mouse-wheel, I want to zoom in to the point of the mouse cursor. I think you can see something similar in the Paradox Games (EU4 etc), where you zoom out, hold your mouse over a country off to the left of the screen, then zoom in and the country zooms in and becomes centered.

Here's my basic script so far, attached to my camera, which just zooms in and out in a straight line.

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

public class CameraZoom : MonoBehaviour {

public float zoom = 10F;

void Update () {
    if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
    {
        zoom -= 1;
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
    {
        zoom += 1;
    }

    GetComponent<Camera>().orthographicSize = zoom;

  }
}

Upvotes: 1

Views: 4326

Answers (2)

KYL3R
KYL3R

Reputation: 4073

Calculate the difference in world space and move towards it while zooming.

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

public class CameraZoom : MonoBehaviour {

public float zoom = 10F;

void Update () {
    if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
    {
        zoom -= 1;
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
    {
        zoom += 1;
    }

     Camera cam = GetComponent<Camera>();
     RaycastHit hit;
     Ray ray = cam.ScreenPointToRay(Input.mousePosition);
     Vector3 targetPos;

     if (Physics.Raycast(ray , out hit))
     {
        targetPos = hit.point;
     }

Camera cam = GetComponent();

    cam.orthographicSize = zoom;
    cam.transform.position += (targetPos - transform.position) / 5f;

  }
}

Upvotes: 2

Umair M
Umair M

Reputation: 10720

I haven't tried this myself but I hope this will work.

Vector3 newPosition;
bool canZoom;
bool isMoving;

void Update () 
{
    if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
    {
        zoom -= 1;
        canZoom = true;
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
    {
        zoom += 1;
        canZoom = true;
    }

     if (canZoom) 
     {
        isMoving = true;
        Camera.main.orthographicSize = zoom;
        newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     }
     if (isMoving)
     {
        transform.position = Vector3.Lerp (transform.position,  newPosition, Time.deltaTime);
     }

     if(transform.position == newPosition)
     {
         isMoving = false;
     }

}

Note: If you want the camera to maintain certain camera height, adjust newPosision's y value before moving.

Hope this helps.

Upvotes: 4

Related Questions