- Home /
Question by
Zottic · Jun 09, 2021 at 11:45 PM ·
unity 5scripting problemcamera-movement
Camera Panning script is inconsistent
I have this script that lets the camera move in the x and z axis but the movement is inconsistent. It gets exponentially harder to move in the positive z and positive x axes and gets exponentially easier to move in the negative z and x axes.
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PerspectivePan : MonoBehaviour {
private Vector3 touchStart;
public Camera cam;
public float groundZ = 0;
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)){
touchStart = GetWorldPosition(groundZ);
}
if (Input.GetMouseButton(0)){
Vector3 direction = touchStart - GetWorldPosition(groundZ);
cam.transform.position += new Vector3(direction.x, 0, direction.z);
}
}
private Vector3 GetWorldPosition(float z){
Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
Plane ground = new Plane(cam.transform.forward, new Vector3(0,0,z));
float distance;
ground.Raycast(mousePos, out distance);
return mousePos.GetPoint(distance);
}
}
This is the camera:
screenshot-67.png
(338.0 kB)
Comment