简体   繁体   English

如何在 Unity 中将相机平移到单击的 3d 对象

[英]How to pan camera to the clicked 3d object in Unity

I need to pan/move camera to a 3d object, when I click on the 3d object.当我单击 3d 对象时,我需要将相机平移/移动到 3d 对象。 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()
    {
        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM