代码之家  ›  专栏  ›  技术社区  ›  koryakinp

Unity3d自顶向下相机控制

  •  0
  • koryakinp  · 技术社区  · 7 年前

    我有一个自上而下的游戏 玩家可以控制汽车的地方。现在,照相机将汽车保持在屏幕中间,并旋转到汽车指向的方向。

    我就是这样做的:

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

    我想改变摄像头的位置,使汽车始终位于屏幕底部:

    enter image description here

    怎么做?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Savaria    7 年前

    如果汽车在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;
    
        2
  •  0
  •   Eissa    7 年前

    您需要做的是找出汽车在屏幕底部的位置偏移量,并将其作为Z轴偏移量应用到您的屏幕中 FixedUpdate()

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

    一个相当简单和粗略的方法是,在游戏模式下,移动汽车游戏对象,使其位于游戏窗口底部的位置。然后使用汽车游戏对象变换组件Z轴上的值作为粗略偏移。

    祝你好运!