![](/img/trans.png)
[英]Is there a way to make a the camera orbit around a GameObject while updating rotation?
[英]Orbit around a gameobject in unity
在Unity中,我有一個具有以下結構的相機
GameObject Pan
=> GameObject Rotation
=> GameObject Zoom
=> GameObject Camera (the component that have the camera script)
我有一個對象目標
GameObject target;
我想圍繞該對象沿所有方向旋轉。 就像是一架飛行的飛機一樣,所以我可以左右旋轉。
我有以下代碼
void ChangePosition()
{
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if (target)
{
transform.RotateAround(target.transform.position, transform.right, -Input.GetAxis("Vertical") * speed);
transform.RotateAround(target.transform.position, transform.up, -Input.GetAxis("Horizontal") * speed);
rotationObject.transform.LookAt(target.transform.position);
}
}
}
當您僅向左或向上移動時,此功能非常有用。
但是,如果您同時移動展位,則旋轉不再直觀。 它還會旋轉照相機。
我希望相機僅向左或向上移動(所以繞2軸旋轉,但不對自己旋轉)
我該如何解決?
我不喜歡使用RotateAround()
但是它們是一個更簡單的解決方案。 首先修改層次結構,添加一個新對象,該對象是旋轉中心,並將其放置在目標對象的中心。
GameObject Target (your target game object)
=> GameObject RotationCenter (local position: 0, 0, 0. The center of Target)
=> GameObject Camera
然后只需旋轉您的新游戲對象RotationCenter
。 這樣的事情應該可以解決問題:
RotationCenter.Rotate(Input.GetAxis("Vertical") * speed, Input.GetAxis("Vertical") * speed);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.