用相机和改变高度的轨道目标

时间:2017-01-29 17:40:28

标签: unity3d camera

我希望能够使用操纵杆(左右)使相机围绕目标进行轨道运动。我用以下代码处理了这个问题:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class OrbitCamera : MonoBehaviour {

    public Transform target;

    public float turnSpeed;
    public float height;
    public float distance;

    private Vector3 offsetX;

    void Start()
    {
        offsetX = new Vector3 (0, height, distance);
    }

    void LateUpdate()
    {
        offsetX = Quaternion.AngleAxis (CrossPlatformInputManager.GetAxis ("hOrbit") * turnSpeed, Vector3.down) * offsetX;

        transform.position = target.position + offsetX;
        transform.LookAt (target.position);
    }
}

我需要扩展此脚本,以便玩家可以使用相同的操纵杆上下移动相机。所以我想要的是能够以球形移动玩家的相机,总是看着玩家。提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用臂式摄像机

boom camera非常容易设置并具有一系列方便的电影控制:

Visualise a camera boom and things become much simpler

要设置它,您只需创建一个我们称之为dolly的新游戏对象。然后你只需将相机放在小车上,然后整理位置和旋转:

<强>相机

  • 转换:0,0, - 距离
  • 轮换:0,0,0

<强>多利

  • 变换:0,高度,0
  • 轮换:0,0,0

为什么繁荣相机很棒

  • 在x和y上旋转小车,你会让相机在球体内移动。 (请注意,在场景视图中拖动旋转Gizmo不会因轴对齐而正确显示效果;仅在检查器中编辑x / y旋转值

  • - 距离 z值是 zoom 。更改相机的本地位置以放大/缩小。

  • 本地旋转相机会产生有趣的倾斜/平移效果。

让它在你的情况下工作

您将脚本附加到小车游戏对象,然后使用操纵杆输入在x / y上旋转:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class OrbitCamera : MonoBehaviour {
    // Height and distance are now set in the scene (by positioning the dolly and changing that camera z value).
    public float turnSpeed;
    private float horizontal;
    private float vertical;

    void LateUpdate()
    {
        // Update horizontal/ vertical angles:
        horizontal += CrossPlatformInputManager.GetAxis ("hOrbit") * turnSpeed;

        vertical += CrossPlatformInputManager.GetAxis ("vOrbit") * turnSpeed;

        // Update the rotation:
        transform.rotation = Quaternion.Euler(horizontal, vertical, 0f);
    }
}