- Home /
Why the movement of the camera in the Z axis is so fast?
This code suposedly make your camera move while pressing the middle mouse button or the right mouse button on the background, in 2D work like a charm, but i'm modifing it to work also in 3D and so-far i can't fix a bug about it, is, because of that, necesary to make it work.
Why the movement of the camera in the Z axis is so fast?
void UpdateCameraMovement(Vector3 lastFramePosition, Vector3 currFramePosition, bool is3D = false)
{
ProcessCameraMovement(lastFramePosition, currFramePosition, is3D);
FixCameraMovement(is3D);
}
//Why so fast with the movement in Z axis?
private void ProcessCameraMovement(Vector3 lastFramePosition, Vector3 currFramePosition, bool is3D = false)
{
try
{
if (is3D)
{
//if(Physics.Raycast(ray, out hit))
//{
//currFramePosition = hit.transform.gameObject.transform.position;
currFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currFramePosition.y = 0;
//}
}
else
{
currFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currFramePosition.z = 0;
}
// Handle screen dragging
if (Input.GetMouseButton(1) || Input.GetMouseButton(2))
{ // Right or middle mouse Button
Vector3 diff = lastFramePosition - currFramePosition;
Camera.main.transform.Translate(diff);
Debug.Log (Camera.main.transform.position+ " Es la posicion del mouse en este momento");
}
// Work Zoom in, Zoom out
Camera.main.orthographicSize += Camera.main.orthographicSize * Input.GetAxis("Mouse ScrollWheel") * -1;
//Limits of the Zoom
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, 1f, 125f);
// Save the mouse position from this frame
// We don't use currFramePosition because we may have moved the camera.
//lastFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//lastFramePosition.z = 0;
//lastFramePosition = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0f);
}
catch(Exception ex)
{
Debug.Log("UpdateCameraMovement(Vector3, Vector3) Error: "+ex.ToString());
}
}
private void FixCameraMovement(bool is3D = false)
{
lastFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(is3D)
{
lastFramePosition.y = 0;
}
else
{
lastFramePosition.z = 0;
}
}
Feel free to ask for comments, clarify stuffs and constructive critics, anything than help to get close to
Thanks in advance
Your answer
![](https://koobas.hobune.stream/wayback/20220612132640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is Input.mousePosition returning wired values using Cinemachine 2D with dynamic following? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Script for camera movement using Arcball,Scripting Camera Movements with Arcball 0 Answers
camera does not move behind the player when switching to them? 0 Answers