- Home /
Question by
JoanneChilds · Mar 21, 2018 at 06:58 PM ·
c#cameracamera-movementcamera rotationpanning
I need to find a way to pan my camera around an object.
I have a map which the camera moves around but need to be able to pan around the map, holding down the right mouse button. I have tried so many online solutions already but none of them seem to work. Could anyone help please?
This is the code I have at the moment for moving the camera...
using UnityEngine;
using System.Collections;
public class CameraDrag3: MonoBehaviour {
float speed = 20.0f;
void Update () {
if (Input.GetMouseButton (0)) {
// Start with our current location
Vector3 newPosition = transform.position;
// Increment based on the mouse motion
newPosition += transform.right * Input.GetAxisRaw ("Mouse X") * Time.deltaTime * speed;
newPosition += transform.up * Input.GetAxisRaw ("Mouse Y") * Time.deltaTime * speed;
// Reassign the vector
transform.position = newPosition;
}
//if (Input.GetMouseButton (1)) {
//}
}
}
Like I said I have tried so many solutions already but cannot get any to work, could someone please help?
Comment
Answer by Rexro · Mar 21, 2018 at 08:55 PM
Here's the script I use to pan around my map:
if (Input.GetMouseButton(0))
{
float mX, mY;
mX = -Input.GetAxis("Mouse X");
mY = -Input.GetAxis("Mouse Y");
gameObject.transform.Translate(Vector3.forward * mY * 3);
gameObject.transform.Translate(Vector3.right * mX * 3);
}