qq_37296877 2017-05-20 11:12 采纳率: 61%
浏览 950

求大神, unity的问题

using UnityEngine;

public class CameraControl : MonoBehaviour
{
public float m_DampTime = 0.2f; // Approximate time for the camera to refocus.
public float m_ScreenEdgeBuffer = 4f; // Space between the top/bottom most target and the screen edge.
public float m_MinSize = 6.5f; // The smallest orthographic size the camera can be.
[HideInInspector] public Transform[] m_Targets; // All the targets the camera needs to encompass.

private Camera m_Camera;                        // Used for referencing the camera.
private float m_ZoomSpeed;                      // Reference speed for the smooth damping of the orthographic size.
private Vector3 m_MoveVelocity;                 // Reference velocity for the smooth damping of the position.
private Vector3 m_DesiredPosition;              // The position the camera is moving towards.


private void Awake ()
{
    m_Camera = GetComponentInChildren<Camera> ();
}


private void FixedUpdate ()
{
    // Move the camera towards a desired position.
    Move ();

    // Change the size of the camera based.
    Zoom ();
}


private void Move ()
{
    // Find the average position of the targets.
    FindAveragePosition ();

    // Smoothly transition to that position.
    transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
}


private void FindAveragePosition ()
{
    Vector3 averagePos = new Vector3 ();
    int numTargets = 0;

    // Go through all the targets and add their positions together.
    for (int i = 0; i < m_Targets.Length; i++)
    {
        // If the target isn't active, go on to the next one.
        if (!m_Targets[i].gameObject.activeSelf)
            continue;

        // Add to the average and increment the number of targets in the average.
        averagePos += m_Targets[i].position;
        numTargets++;
    }

    // If there are targets divide the sum of the positions by the number of them to find the average.
    if (numTargets > 0)
        averagePos /= numTargets;

    // Keep the same y value.
    averagePos.y = transform.position.y;

    // The desired position is the average position;
    m_DesiredPosition = averagePos;
}


private void Zoom ()
{
    // Find the required size based on the desired position and smoothly transition to that size.
    float requiredSize = FindRequiredSize();
    m_Camera.orthographicSize = Mathf.SmoothDamp (m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
}


private float FindRequiredSize ()
{
    // Find the position the camera rig is moving towards in its local space.
    Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);

    // Start the camera's size calculation at zero.
    float size = 0f;

    // Go through all the targets...
    for (int i = 0; i < m_Targets.Length; i++)
    {
        // ... and if they aren't active continue on to the next target.
        if (!m_Targets[i].gameObject.activeSelf)
            continue;

        // Otherwise, find the position of the target in the camera's local space.
        Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);

        // Find the position of the target from the desired position of the camera's local space.
        Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;

        // Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
        size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));

        // Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
        size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
    }

    // Add the edge buffer to the size.
    size += m_ScreenEdgeBuffer;

    // Make sure the camera's size isn't below the minimum.
    size = Mathf.Max (size, m_MinSize);

    return size;
}


public void SetStartPositionAndSize ()
{
    // Find the desired position.
    FindAveragePosition ();

    // Set the camera's position to the desired position without damping.
    transform.position = m_DesiredPosition;

    // Find and set the required size of the camera.
    m_Camera.orthographicSize = FindRequiredSize ();
}

}

Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
这一句转换了世界坐标到本地坐标, 不知道效果是什么,求大神解释下
还有上面averagePos求得是几何中心,为什么要求几何中心,也叫重心,有什么用呢。几何中心有什么特殊的性质吗。求大神帮忙普及一下知识

  • 写回答

2条回答

  • 魏甲鑫 2018-06-19 04:58
    关注

    假如说现在有三个或多个物体,摄像机追踪时,目标位置是几个点的"中间",这个就是重心,你可以去看看跳一跳的相机就知道了,他不是正正的跟在角色身上的

    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题