Hey there… putting a call out for anyone that can help me with a Click Drag script to move a camera like in FarmVille and other similar type games. Cany anyone help?
No.
I’m not sure that this will work or not… but check this…
var MousePressed:boolean=false;
var X:float;
var Y:float;
function Update()
{
if(Input.GetMouseButtonDown(0))
{
MousePressed=true;
X=Input.mousePosition.x;
Y=Input.mousePosition.y;
}
if(Input.GetMouseButtonUp(0)
{
MousePressed=false;
}
if(MousePressed==true)
{
GameObject.Find("Main Camera").transform.position.x=(-1)*(Input.mousePosition.x-X);
GameObject.Find("Main Camera").transform.position.y=(-1)*(Input.mousePosition.y-Y);
X=Input.mousePosition.x;
Y=Input.mousePosition.y;
}
}
I will try it now! Thank you very much for your help!
It looks like the script is starting to move the camera, but then it re-centers immediately right back to the starting position. So is it that the script is not accounting for the space it has already traversed? I really appreciate the help… any idea’s?
Slightly better version of same script:
var MousePressed=false;
var X:float;
var Y:float;
var cam:Camera;
function Update()
{
if(Input.GetMouseButtonDown(0))
{
MousePressed=true;
X=Input.mousePosition.x;
Y=Input.mousePosition.y;
}
if(Input.GetMouseButtonUp(0))
{
MousePressed=false;
}
if(MousePressed==true)
{
cam.transform.position.x+=(-1)*(Input.mousePosition.x-X);
cam.transform.position.y+=(-1)*(Input.mousePosition.y-Y);
X=Input.mousePosition.x;
Y=Input.mousePosition.y;
}
}
Another Version:
var cam:Camera;
var Damper : float = 0.05;
function Start()
{
MoveOnClick();
}
function MoveOnClick()
{
var lastMouse : Vector2;
while(true){
lastMouse = Input.mousePosition;
yield;
if(Input.GetMouseButton(0))
cam.transform.position += (-1)*(Input.mousePosition-lastMouse)*Damper;
}
}
You must of missed my version of the code - I just updated it.
Your code looks fine, but it is more of a ‘jump’ to position, then drag. ( That said I haven’t actually played games in OP).
You also need to have a damper like I included (else 1 pixel becomes 1 meter in unity!)
And lastly, while I like the fact you converted the 2d into a top down 3d, (on my testing I simply ‘zoom in’ and out cause of my camera :), you meant to put Input.mousePosition.y not .z!
Edit:
Also noticed:
Haven’t factored in the coordinate system (mouse starts at bottom left, while we need it to start from center).
No drag at all, if I click 5,10 and then on next frame my mouse is on 6, 11 - you will move me 11,21 not 1,1.
Change these two lines… in my code above…
GameObject.Find("Main Camera").transform.position.x-=Input.mousePosition.x-X;
GameObject.Find("Main Camera").transform.position.y-=Input.mousePosition.y-Y;
This will work fine…
Ok… further question… the solution you guys offered before causes the mouse to “slip” off the colliders if the mouse movement is too fast! Is there another function we can call this code from that will prevent this from happening?
The collider that the game object has (the object you are trying to manipulate).