簡體   English   中英

帶有 Unity 3D 的 C#:當用戶移動鼠標時,如何讓相機圍繞對象移動

[英]C# with Unity 3D: How do I make a camera move around an object when user moves mouse

我正在嘗試在 Unity 4 中進行 3d 查看模擬,用戶可以在其中選擇一個對象並移動鼠標以圍繞它旋轉(360 度)我已經拍攝了很多照片來嘗試讓它工作,但我每次都失敗了,任何將不勝感激,如果它是用 C# 編寫的,那就太好了! (但它不必)提前致謝!

這是一種不同而有趣的方式:)(我使用它)

截屏

(這里,立方體是目標)

1)創建球體 - 名稱:“相機軌道” - 添加材質:透明(Alpha = 0) - 根據需要縮放 - 旋轉:(0,0,0.1f)
2) 將相機作為“子項”添加到相機軌道表面。 位置 = (0, "y = 相機軌道比例",0) 旋轉 = (90,0,0)
3) 創建空游戲對象 - 名稱:輸入控件。

輸入控件.cs:

public class InputControl : MonoBehaviour
{
   public GameObject cameraOrbit;

   public float rotateSpeed = 8f;

   void Update()
   {
       if (Input.GetMouseButton(0))
       {
           float h = rotateSpeed * Input.GetAxis("Mouse X");
           float v = rotateSpeed * Input.GetAxis("Mouse Y");

           if (cameraOrbit.transform.eulerAngles.z + v <= 0.1f || cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                v = 0;

           cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);
       }

       float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

       if (scrollFactor != 0)
       {
           cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);
       }

   }
}

相機控制器.cs:

public class CameraController : MonoBehaviour
{
   public Transform cameraOrbit;
   public Transform target;

   void Start()
   {
       cameraOrbit.position = target.position;
   }

   void Update()
   {
       transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0);

       transform.LookAt(target.position);
   }
}

4) 將 CameraController.cs 添加到相機。
5) 將 InputControl.cs 添加到 Input Control。
6) 在腳本中設置公共變量。 (“相機軌道”和“目標”)

就這樣。 鼠標單擊並拖動:旋轉 - 鼠標輪:放大縮小。

附: 如果需要,您可以將目標更改為運行時。

MouseOrbit 腳本執行此操作:

http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

只需將此腳本附加到您的相機對象中,然后在檢查器中鏈接目標對象即可。

太棒了。 我所做的唯一更改是向 Camera Orbit 添加腳本:

public class FollowPlayer : MonoBehaviour {

    public GameObject player;
    private Vector3 playerPos;

    // Update is called once per frame
    void Update () {
        if (this.transform.localScale.x <= 1)
        {
            this.transform.localScale = new Vector3(1, 1, 1);
        }

        if (this.transform.localScale.x >= 15)
        {
            this.transform.localScale = new Vector3(15, 15, 15);
        }

        playerPos = player.transform.position;
        this.transform.position = playerPos;
    }
}

然后將您的“播放器”對象附加到輸入控件,輸入控件將轉到播放器的任何位置,允許您跟蹤播放器,以及旋轉和鼠標滾輪縮放。 想要。

localScale if 語句意味着您只能放大和縮小到目前為止。

這個腳本現在唯一的問題是,如果你縮小到 15,然后繼續嘗試縮小,相機會反彈。 我相信這很容易解決,不過,我還沒有花時間。

-- 將其用於鼠標按下並拖動 -- 我在這里修改了代碼: http : //wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;

public float yMinLimit = -20f;
public float yMaxLimit = 80f;

public float distanceMin = .5f;
public float distanceMax = 15f;

private Rigidbody rigidbody;

float x = 0.0f;
float y = 0.0f;

float mouseX = 0f;
float mouseY = 0f;

// Use this for initialization
void Start()
{
    Vector3 angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    rigidbody = GetComponent<Rigidbody>();

    // Make the rigid body not change rotation
    if (rigidbody != null)
    {
        rigidbody.freezeRotation = true;
    }
}

void LateUpdate()
{
    if (target)
    {
        GetMouseButtonDown_XY();

        x += mouseX * xSpeed * distance * 0.02f;
        y -= mouseY * ySpeed * 0.02f;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        Quaternion rotation = Quaternion.Euler(y, x, 0);

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

        RaycastHit hit;
        if (Physics.Linecast(target.position, transform.position, out hit))
        {
            distance -= hit.distance;
        }
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }
}

public static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360F)
        angle += 360F;
    if (angle > 360F)
        angle -= 360F;
    return Mathf.Clamp(angle, min, max);
}

Vector3 mousePosPrev;
void GetMouseButtonDown_XY()
{
    if (Input.GetMouseButtonDown(0))
    {
        mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    } 

    if (Input.GetMouseButton(0))
    {
        Vector3 newMousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);

        if (newMousePos.x < mousePosPrev.x)
        {
            mouseX = -1;
        } else if (newMousePos.x > mousePosPrev.x)
        {
            mouseX = 1;
        } else
        {
            mouseX = -0;
        }

        if (newMousePos.y < mousePosPrev.y)
        {
            mouseY = -1;
        }
        else if (newMousePos.y > mousePosPrev.y)
        {
            mouseY = 1;
        }
        else
        {
            mouseY = -0;
        }

        mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    }
}

您根本不需要 CameraController,只需將相機的 z 旋轉設置為 -90。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM