- Home /
Move Camera once Mouse Drag is complete
Hi,
Here is the script I currently have. The script is attached to the camera.
Right now the moving works, if I left click and drag the mouse upwards it moves upwards. But once I rotate my camera 180 degree (there is a script which manages rotation and zoom), the camera moves downwards when mouse is upwards. And in between 0 and 180 degrees the camera goes in different directions.
Is there anyway to make the camera act like the one within Unity's Scene Editor? (with middle mouse button). Where if I click and drag downwards it moves upwards? But instead of moving X and Y axis move X and Z axis.
The TargetLookAt object is where the camera is focused on and the object itself doesn't rotate. It should just move so the camera will follow it. (the following scripts are all done and working).
private Vector3 mouseOrigin;
private bool isPanning;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
mouseOrigin = Input.mousePosition;
//right click was pressed
isPanning = true;
}
// cancel on button release
if (!Input.GetMouseButton(0))
{
// if(isPanning)
// MoveTarget(mouseOrigin, Input.mousePosition);
isPanning = false;
}
if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Debug.Log(pos.ToString());
Vector3 move = new Vector3(pos.x * MoveSpeed, 0, pos.y * MoveSpeed);
TargetLookAt.transform.Translate(move, Space.World);
}
}
Well the solution was easy. By just rotating the TargetLookAt to the rotation of camera, the movements become aligned.
TargetLookAt.transform.rotation = Quaternion.Euler(0, (Camera.main.transform.eulerAngles.y), 0);
Answer by toddisarockstar · Mar 06, 2017 at 04:49 AM
this script on a camera would move kinda like the unity editor camera. hopefully you can adjust from here:
float mx;
float my;
Vector3 v;
void Start () {
}
// Update is called once per frame
void Update () {
mx = Input.GetAxis ("Mouse X");
my = Input.GetAxis ("Mouse Y");
if (Input.GetMouseButton(2)) {
v = new Vector3 (mx, 0, my);
transform.position = transform.position + v * .5f;
}
if (Input.GetMouseButton(1)) {
v = new Vector3 (my, mx, 0);
transform.eulerAngles = transform.eulerAngles + v;
}
}
Hey,
thanks. This gives the same result as before. Once I rotate the camera the controls work in the opposite direction and in between rotations the controls are completely a mess. The camera is rotated via Transform.LookAt
and a distance between the the TargetLookAt and Camera is kept by the scripts as well. Both the camera and TargetLookAt are free objects without any parent or child.
https://i.gyazo.com/d572268e670bf98aec6545df2489165f.mp4 (See how the camera moves in different direction once I turn 90 degrees)
Answer by eldruz · Mar 06, 2017 at 01:23 PM
As an alternative, you could also replace the final line of the script with :
TargetLookAt.transform.Translate(move, Camera.main.transform);
This way, the translation is always relative to the camera local coordinates system.
Your answer
Follow this Question
Related Questions
I need to make the Camera Quit snapping to center... 1 Answer
ps3 controller messed up the normal controls 1 Answer
Help Ship Controll 0 Answers
rotate with alt doesnt work 15 Answers
Simple Object Dragging 0 Answers