2
\$\begingroup\$

I have a basic camera setup which is angled like so:

x: 30
y: 0 
z: 0
Projection: Orthographic

And i attached a C# component to it and made it so the camera moves with a right click and drag movement.

The problem is its not moving at the same amount as the mouse. I had to set a value for scroll speed to 1000 and even then its still too slow.

I want the camera to move with the mouse in a more raw amount, then i wish to add some lerp effect to it so it feels a bit more slick.

I don't know why the movement is so slow, i come from a 2D background and am learning 3D so i suspect my lack of understanding of 3D positions might be the issue.

This is my current code:

using UnityEngine;
using System.Collections;

public class cameraMove : MonoBehaviour {


// VARIABLES
public float panSpeed = 1000.0f;

private Vector3 mouseOrigin;
private bool isPanning;


void Start ()
{
    Vector3 pos = GameObject.Find("Sun").transform.position;
    Camera.main.transform.LookAt (pos);
}

// Update is called once per frame
void Update ()
{


    if (Input.GetMouseButtonDown (1)) 
    {
        //right click was pressed   
        mouseOrigin = Input.mousePosition;
        isPanning = true;
    }


    // cancel on button release
    if (!Input.GetMouseButton (1)) 
    {
        isPanning = false;
    }

    //move camera on X & Y
    if (isPanning) 
    {
        Vector3 pos     = Camera.main.ScreenToViewportPoint (mouseOrigin-Input.mousePosition);

        // update x and y but not z
        Vector3 move    = new Vector3 (pos.x * panSpeed, pos.y * panSpeed, 0);

        Camera.main.transform.Translate (move, Space.World);
        mouseOrigin = Input.mousePosition;
    }
}

}

\$\endgroup\$
1
  • \$\begingroup\$ You code should work fine, in fact 1000 will get you to the north pole \$\endgroup\$ Commented Mar 10, 2016 at 10:32

2 Answers 2

0
\$\begingroup\$

Input.GetAxisRaw might be what you're looking for. In the Input Manager (Edit -> Project Settings -> Input) setup a variable for "Mouse X" and "Mouse Y" (if not already setup [as below]).

enter image description here

Then in code;

public class CameraMove : MonoBehaviour
{
    private Vector2 pos;
    private bool    isPanning;

    public void Start()
    {
        pos = new Vector2();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
            isPanning = true;

        if (Input.GetMouseButtonUp(1))
            isPanning = false;

        if (isPanning)
        {
            pos.Set(Input.GetAxisRaw("Mouse X"), -Input.GetAxisRaw("Mouse Y"));
            transform.Translate(pos, Space.World);
        }
    }
}

For movement in the Y plane, the value from the mouse must be negated to, unless you with to use inverted controls. Hope this helped.

\$\endgroup\$
2
  • \$\begingroup\$ Hi thanks for the suggestion i tried this method doing this: pastebin.com/9CRhWag8 But it doesn't quite work, the camera just jitters in the same spot, but not actually scroll. What might i have gotten wrong? \$\endgroup\$ Commented Mar 11, 2016 at 2:06
  • \$\begingroup\$ Updated my answer for code example, you were very close; you didn't need to save the origin of the mouse, as GetAxisRaw returns the current movement of the mouse for that frame. I also swapped !InputGetButton(1) for Input.GetMouseButtonUp(1), (the functionality you were looking for). Hope this solves your problem, let me know if you need any more help. \$\endgroup\$ Commented Mar 11, 2016 at 15:38
0
\$\begingroup\$

I think you can compare Input.mousePosition (in pixels) directly:

static Vector3 mouseDelta;
//from (-Screen.width, -Screen.height, 0) to (Screen.width, Screen.height, 0)
mouseDelta = (mouseOrigin - Input.mousePosition);
//convert pixels to world-units
mouseDelta /= panSpeed;
Camera.main.transform.Translate(mouseDelta, Space.World);
mouseOrigin = Input.mousePosition;

Note: you should clamp the transform's position to valid coordinates.

\$\endgroup\$
3
  • \$\begingroup\$ Why do i need to divide by panSpeed? I want to be able to move the camera without the need for a variable so it will move with my mouse in a 1 to 1 way. \$\endgroup\$ Commented Mar 10, 2016 at 4:44
  • \$\begingroup\$ @wduk, you don't have to; 1:1 is ok if it works for you. I think the problem is ScreenToViewportPoint. \$\endgroup\$ Commented Mar 10, 2016 at 4:46
  • \$\begingroup\$ I will check it out after work to see if I can get it working and will let you know. \$\endgroup\$ Commented Mar 10, 2016 at 6:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.