- Home /
Click hold/drag move camera
Hello Unity peeps,
I'm pretty new at Unity and I'm trying to develop my first game. I've completed the course on lynda.com on Unity and I have a basic understanding of the software. I'm a web developer and I'm seasoned in PHP and Javascript so I use Javascript for my language in Unity. What I'm trying to create is game where the 3rd person army crawls across a plane.
Here's what I have:
It's a plane with a camera facing it on orthographic mode. I have rocks (spheres) where you will click and drag down and it will pull the 3rd person along. So, I need the camera to go the exact opposite way the user is dragging. When you click and drag the camera exposes more area ahead with more rocks and you will grab the next one with the opposite hand on the 3rd person rock to rock to rock.
Here's what I understand I need so far:
When I click that rock, I need to transfer from screen to world. I need the starting position of the rock click. This is the first problem I encountered. How do I get that first position? I can get the x and y position of the camera but it changes as it moves. How do I capture the very first point of the cameras position and save it in a variable to use? I figure as the user drags down on the screen, the position of the mouse can be reversed and put into the camera so it goes the exact opposite way. Any pointers in the right direction would help me out so much. I will post the update function I have right now in my click.js file on each of the spheres (rocks).
function Update () {
if (Input.GetMouseButtonDown(0))
{
var mousePos = Input.mousePosition;
var cam = camera.main;
var v = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
Debug.Log(v.y);
Camera.main.transform.position.y = v.y;
}
Answer by Curdflappers · Aug 28, 2016 at 12:21 AM
I know I'm waaay late to the party but I've modified Franco Marini's script and cut down a few lines. Same functionality, but a bit dryer and more readable.
private Vector3 ResetCamera; // original camera position
private Vector3 Origin; // place where mouse is first pressed
private Vector3 Diference; // change in position of mouse relative to origin
void Start()
{
ResetCamera = Camera.main.transform.position;
}
void LateUpdate()
{
if(Input.GetMouseButtonDown(0))
{
Origin = MousePos();
}
if (Input.GetMouseButton(0))
{
Diference = MousePos() - transform.position;
transform.position = Origin - Diference;
}
if (Input.GetMouseButton(1)) // reset camera to original position
{
transform.position = ResetCamera;
}
}
// return the position of the mouse in world coordinates (helper method)
Vector3 MousePos()
{
return Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
Answer by sparkzbarca · Jan 25, 2013 at 11:21 PM
since your using orthographic camera im assuming your saying your basically making a 2d game.
It sounds like your "looking down" on the plane. As such your mouse exists above it.
That means to find the rock when they click you would cast a ray down.
Down can be represented simply as -Vector3.up
to get a direction you take the 2d x and y values of the mouse
and make x stay x but because your looking down. The Y value is depth or Z. then you cast from camera.transform.position.y to camera.transform.position.y + (-vector3.down) which is the same as saying (0,-1,0)
so
ray.origin = new Vector3(mouseposition.x,camera.transform.positon.y, mouseposition.z);
ray.direction = ray.origin + (-Vector3.up); or just ray.origin - Vector3.up if you want.
that casts straight down from the mouse to whatever is on the ground directly below it. thats if you want to know whats under the mouse for some reason.
Now for how to drag a camera.
if you want the camera to travel in the opposite direction of the mouse input.
every frame
input.getaxis("Mouse X") & input.getaxis("Mouse Y")
return how much the mouse has moved on that axis in that frame so if your wondering where the camera should be each frame if it should move exactly the opposite of the mouse.
vector3 CameraPos;
Float MouseX;
Float MouseY;
MouseX = input.getaxis("Mouse X");
MouseY = input.getaxis("Mouse Y");
CameraPos = new vector3(- MouseX, 0, - MouseY);
camera.transform.position += CameraPos;
Notice we used - in front of the mousex and y to reverse the values.
also notice we didnt reset the whole value of the camera position we just added to it the opposite of the mouse input.
lastly notice we didnt change the Y value at all. the cameras height should be constant and the mouse should cause the camera to move along that axis.
MARK AS ANSWERED and have a nice day :)
Answer by Franco-Marini · Jan 28, 2015 at 10:17 PM
Sorry if im late. this c++ script drags the camera.
public class CameraDragMove : MonoBehaviour {
private Vector3 ResetCamera;
private Vector3 Origin;
private Vector3 Diference;
private bool Drag=false;
void Start () {
ResetCamera = Camera.main.transform.position;
}
void LateUpdate () {
if (Input.GetMouseButton (0)) {
Diference=(Camera.main.ScreenToWorldPoint (Input.mousePosition))- Camera.main.transform.position;
if (Drag==false){
Drag=true;
Origin=Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
} else {
Drag=false;
}
if (Drag==true){
Camera.main.transform.position = Origin-Diference;
}
//RESET CAMERA TO STARTING POSITION WITH RIGHT CLICK
if (Input.GetMouseButton (1)) {
Camera.main.transform.position=ResetCamera;
}
}
}