I need to pan/move camera to a 3d object, when I click on the 3d object. I found the below script. but when I attach it to an object where ever I click the camera move to there. how to fix this. below is the script
using UnityEngine;
public class CamTest : MonoBehaviour {
Vector3 groundCamOffset;
Vector3 camTarget;
Vector3 camSmoothDampV;
private Vector3 GetWorldPosAtViewportPoint(float vx, float vy) {
Ray worldRay = camera.ViewportPointToRay(new Vector3(vx, vy, 0));
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float distanceToGround;
groundPlane.Raycast(worldRay, out distanceToGround);
Debug.Log("distance to ground:" + distanceToGround);
return worldRay.GetPoint(distanceToGround);
}
void Start() {
Vector3 groundPos = GetWorldPosAtViewportPoint(0.5f, 0.5f);
Debug.Log("groundPos: " + groundPos);
groundCamOffset = camera.transform.position - groundPos;
camTarget = camera.transform.position;
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
// Center whatever position is clicked
float mouseX = Input.mousePosition.x / camera.pixelWidth;
float mouseY = Input.mousePosition.y / camera.pixelHeight;
Vector3 clickPt = GetWorldPosAtViewportPoint(mouseX, mouseY);
camTarget = clickPt + groundCamOffset;
}
// Move the camera smoothly to the target position
camera.transform.position = Vector3.SmoothDamp(
camera.transform.position, camTarget, ref camSmoothDampV, 0.5f);
}
}
I found a way for it. I gave all clickable object same tag and applied this script on the manager
using UnityEngine;
public class CameraPanToSelectedObject : MonoBehaviour {
Vector3 groundCamOffset;
Vector3 camTarget;
Vector3 camSmoothDampV;
//GameObject cam;
public Camera cam;
public GameObject[] hotspots;
Ray ray;
RaycastHit hit;
public float panspeed;
public string colliderTag;
public bool clicked=false;
public Vector3 tempCamTarget;
public Vector3 tempCamPosition;
private Vector3 GetWorldPosAtViewportPoint(float vx, float vy) {
Ray worldRay = cam.ViewportPointToRay(new Vector3(vx, vy, 0));
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float distanceToGround;
groundPlane.Raycast(worldRay, out distanceToGround);
Debug.Log("distance to ground:" + distanceToGround);
return worldRay.GetPoint(distanceToGround);
}
void Start() {
Vector3 groundPos = GetWorldPosAtViewportPoint(0.5f, 0.5f);
Debug.Log("groundPos: " + groundPos);
groundCamOffset = cam.transform.position - groundPos;
camTarget = cam.transform.position;
}
void Update() {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit))
{
colliderTag =hit.collider.tag;
}
}
if (Input.GetMouseButtonDown(0) && colliderTag=="Player" && clicked==false)
{
// Center whatever position is clicked
float mouseX = Input.mousePosition.x / cam.pixelWidth;
float mouseY = Input.mousePosition.y / cam.pixelHeight;
Vector3 clickPt = GetWorldPosAtViewportPoint(mouseX, mouseY);
camTarget = clickPt + groundCamOffset;
clicked =true;
tempCamTarget.x = float.Parse(camTarget.x.ToString("0.##"));
tempCamTarget.y = float.Parse(camTarget.y.ToString("0.##"));
tempCamTarget.z =float.Parse(camTarget.z.ToString("0.##"));
}
// Move the camera smoothly to the target position
if(tempCamPosition!=tempCamTarget && clicked ==true)
{
cam.transform.position = Vector3.SmoothDamp(cam.transform.position, camTarget, ref camSmoothDampV, panspeed);
tempCamPosition.x = float.Parse(cam.transform.position.x.ToString("0.##"));
tempCamPosition.y = float.Parse(cam.transform.position.y.ToString("0.##"));
tempCamPosition.z = float.Parse(cam.transform.position.z.ToString("0.##"));
}
else if(tempCamPosition==tempCamTarget)
{
clicked =false;
colliderTag="";
}
}
void camSmoothDamp()
{
}
}