簡體   English   中英

Unity3d自上而下的相機控件

[英]Unity3d top-down camera control

我在Unity3D中有一個自上而下的游戲,玩家可以在其中控制汽車。 目前,相機將汽車停在屏幕中間,並旋轉到汽車指向的方向。

我就是這樣做的:

public class CameraFollowController : MonoBehaviour
{
    private void FixedUpdate()
    {
        transform.rotation = Quaternion.Euler(90, car.rotation.eulerAngles.y + 90, 90);
        transform.position = new Vector3(car.position.x, cameraHeight, car.position.z);
    }

    public Transform car;
    public float cameraHeight = 10;
}

我想移動攝像頭的位置,因此汽車始終位於屏幕底部:

在此處輸入圖片說明

怎么做 ?

如果汽車在x / y軸上移動,則可以使用transform.forward獲取汽車面向的方向,然后對其進行調整。

public float distance; // How much you want to offset

// Get the direction of the car
Vector3 dir = car.transform.forward;

// Offset the position
transform.position += -dir * distance;

看來您要在Z軸上偏移攝影機的位置。

您需要做的是找出汽車在屏幕底部的位置偏移量,並將其作為您的FixedUpdate()循環中的Z軸偏移量應用。

transform.position = new Vector3(car.position.x, cameraHeight, car.position.z *-/+* zCamOffset);

解決偏移量的一種相當簡單且粗糙的方法是,在游戲模式下,移動汽車GameObject,使其位於游戲窗口底部的位置。 然后,將汽車GameObject的轉換組件在Z軸上的值用作粗偏移。

祝你好運!

暫無
暫無

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

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