|  | 
 
| 如何通过脚本控制摄像机的移动 
   在这个类中,摄像机预设了5个点。当玩家移动的时候,从后先前查询这5个点哪个点可以看到玩家,则将摄像机移到这个新位置。
 
 CameraMovement
 C#
 JS复制代码using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour
{
        public float smooth = 1.5f;         // 摄像机追随速度.
        
        
        private Transform player;           // 玩家变换矩阵变量.
        private Vector3 relCameraPos;       // 摄像机与玩家之间的相对位置向量.
        private float relCameraPosMag;      // 摄像机到玩家之间的距离.
        private Vector3 newPos;             // 摄像机将要到达的新位置.
        
        
        void Awake ()
        {
                // 初始化变量.
                player = GameObject.FindGameObjectWithTag(Tags.player).transform;
                
                // 设置相对位置作为初始位置.
                relCameraPos = transform.position - player.position;
                relCameraPosMag = relCameraPos.magnitude - 0.5f;
        }
        
        
        void FixedUpdate ()
        {
                // 设定一个标准位置从摄像机到玩家.
                Vector3 standardPos = player.position + relCameraPos;
                
                // 摄像机在玩家头顶的高度.
                Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
                
                // 创建一个数组,设定5个位置可以看到玩家.
                Vector3[] checkPoints = new Vector3[5];
                
                // 第一个是摄像机的标准位置.
                checkPoints[0] = standardPos;
                
                // 剩下三个分别是从悬浮位置到标准位置之间的几个位置.
                checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
                checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
                checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
                
                // 最后一个是悬浮位置.
                checkPoints[4] = abovePos;
                
                // 遍历这些点...
                for(int i = 0; i < checkPoints.Length; i++)
                {
                        // ... 如果在当前位置可以看到玩家...
                        if(ViewingPosCheck(checkPoints[i]))
                                // ... 返回.
                                break;
                }
                
                // 从摄像机的当前位置过渡到新位置.
                transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
                
                // 平滑过渡
                SmoothLookAt();
        }
        
        
        bool ViewingPosCheck (Vector3 checkPos)
        {
                RaycastHit hit;
                
                // 如果设想在看到玩家的图中碰到物体...
                if(Physics.Raycast(checkPos, player.position - checkPos, out hit, relCameraPosMag))
                        // ... 这个物体不是玩家...
                        if(hit.transform != player)
                                // 这个位置就不对.
                                return false;
                
                // 如果中间没有任何阻挡.
                newPos = checkPos;
                return true;
        }
        
        
        void SmoothLookAt ()
        {
                // 建立摄像机到玩家的射线.
                Vector3 relPlayerPosition = player.position - transform.position;
                
                // Create a rotation based on the relative position of the player being the forward vector.
                // 获取旋转角
                Quaternion lookAtRotation = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
                
                // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
                // 做插值
                transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
        }
}
复制代码#pragma strict
public var smooth : float = 1.5f;           // The relative speed at which the camera will catch up.
private var player : Transform;             // Reference to the player's transform.
private var relCameraPos : Vector3;         // The relative position of the camera from the player.
private var relCameraPosMag : float;        // The distance of the camera from the player.
private var newPos : Vector3;               // The position the camera is trying to reach.
function Awake ()
{
    // Setting up the reference.
    player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    
    // Setting the relative position as the initial relative position of the camera in the scene.
    relCameraPos = transform.position - player.position;
    relCameraPosMag = relCameraPos.magnitude - 0.5f;
}
function FixedUpdate ()
{
    // The standard position of the camera is the relative position of the camera from the player.
    var standardPos : Vector3 = player.position + relCameraPos;
    
    // The abovePos is directly above the player at the same distance as the standard position.
    var abovePos : Vector3 = player.position + Vector3.up * relCameraPosMag;
    
    // An array of 5 points to check if the camera can see the player.
    var checkPoints : Vector3[] = new Vector3[5];
    
    // The first is the standard position of the camera.
    checkPoints[0] = standardPos;
    
    // The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
    checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
    checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
    checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
    
    // The last is the abovePos.
    checkPoints[4] = abovePos;
    
    // Run through the check points...
    for(var i = 0; i < checkPoints.Length; i++)
    {
        // ... if the camera can see the player...
        if(ViewingPosCheck(checkPoints[i]))
            // ... break from the loop.
            break;
    }
    
    // Lerp the camera's position between it's current position and it's new position.
    transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
    
    // Make sure the camera is looking at the player.
    SmoothLookAt();
}
function ViewingPosCheck (checkPos : Vector3) : boolean
{
    var hit : RaycastHit;
    
    // If a raycast from the check position to the player hits something...
    if(Physics.Raycast(checkPos, player.position - checkPos, hit, relCameraPosMag))
        // ... if it is not the player...
        if(hit.transform != player)
            // This position isn't appropriate.
            return false;
    
    // If we haven't hit anything or we've hit the player, this is an appropriate position.
    newPos = checkPos;
    return true;
}
function SmoothLookAt ()
{
    // Create a vector from the camera towards the player.
    var relPlayerPosition : Vector3 = player.position - transform.position;
    
    // Create a rotation based on the relative position of the player being the forward vector.
    var lookAtRotation : Quaternion = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
    
    // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
    transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
}
 | 
 |