I'm trying to create a RTS-esque camera control system for my game and I've hit a wall and am unsure how to proceed.
What I want to happen is the following:
- Mouse Click + Drag = Move the camera around the world (works)
- Mouse Scroll Wheel Click + Drag = Rotate camera around the world (works)
- Mouse Scroll Wheel = Zoom in/out using MoveToward/Transform.Forward..etc (sorta works)
In another script I have FoV zoom working, but it is not ideal for the type of experience I want to create. Instead I am trying to physically move the camera in the direction it is pointed.
This current script is 90% functional in that it can do all the things listed above, but when I click + drag to move to a new location after scrolling to zoom, the camera resets to the original position.
Here is what happens in Update():
void Update ()
{
currentPosition = transform.position;
scrollAxis = Input.GetAxis ("Mouse ScrollWheel");
if (Input.GetMouseButtonDown (0))
{
mouseOrigin = Input.mousePosition;
isPanning = true;
}
// cancel on button release
if (!Input.GetMouseButton (0))
{
isPanning = false;
}
//move camera on X & Y
else if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3 (pos.x * -panSpeed, pos.y * -panSpeed, 0);
Camera.main.transform.Translate (move, Space.Self);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0 );
transform.localPosition = new Vector3( transform.position.x, transform.position.y - (transform.position.y - (initialPosition.y)), transform.position.z );
}
//rotate camera with mousewheel click
if (Input.GetMouseButton(2)) {
yRotation -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
yRotation = Mathf.Clamp(yRotation, -80, 80);
xRotation += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
xRotation = xRotation % 360;
transform.localEulerAngles = new Vector3(yRotation + initialRotation.x, xRotation + initialRotation.y, 0);
}
//zoom with scroll
if ( Input.GetAxis("Mouse ScrollWheel") > 0 && !isPanning) {
transform.Translate(Vector3.forward * Time.deltaTime * 10000f * scrollAxis, Space.Self );
transform.localPosition = new Vector3( transform.position.x, transform.position.y, transform.position.z );
}
}
Any help is much appreciated, thanks!