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;
}
我想移动相机位置,让汽车始终在屏幕底部:
怎么做?
您似乎在尝试偏移相机在 Z 轴上的位置。
您需要做的是找出屏幕底部存在的汽车的位置偏移量,并将其用作 FixedUpdate()
循环中的 Z 轴偏移量。
transform.position = new Vector3(car.position.x, cameraHeight, car.position.z *-/+* zCamOffset);
计算偏移量的一种相当简单粗略的方法是,在游戏模式下,移动汽车游戏对象,使其位于游戏底部的位置 window。然后使用car GameObject的transform组件Z轴的值作为粗略偏移。
祝你好运!
如果汽车在 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;
我在 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;
}
我想移动相机位置,让汽车始终在屏幕底部:
怎么做?
您似乎在尝试偏移相机在 Z 轴上的位置。
您需要做的是找出屏幕底部存在的汽车的位置偏移量,并将其用作 FixedUpdate()
循环中的 Z 轴偏移量。
transform.position = new Vector3(car.position.x, cameraHeight, car.position.z *-/+* zCamOffset);
计算偏移量的一种相当简单粗略的方法是,在游戏模式下,移动汽车游戏对象,使其位于游戏底部的位置 window。然后使用car GameObject的transform组件Z轴的值作为粗略偏移。
祝你好运!
如果汽车在 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;