簡體   English   中英

如何在 Unity 中將相機平移到單擊的 3d 對象

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

當我單擊 3d 對象時,我需要將相機平移/移動到 3d 對象。 我找到了下面的腳本。 但是當我將它附加到一個物體上時,我點擊相機就會移動到那里。 如何解決這個問題。 下面是腳本

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);
}

}

我找到了方法。 我給了所有可點擊的對象相同的標簽,並在管理器上應用了這個腳本

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