【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1843|回复: 0

6-1 Camera Movement 摄像机的控制

[复制链接]
发表于 2013-10-2 08:19:37 | 显示全部楼层 |阅读模式
如何通过脚本控制摄像机的移动
未标题-1.jpg
在这个类中,摄像机预设了5个点。当玩家移动的时候,从后先前查询这5个点哪个点可以看到玩家,则将摄像机移到这个新位置。

CameraMovement
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class CameraMovement : MonoBehaviour
  4. {
  5.         public float smooth = 1.5f;         // 摄像机追随速度.
  6.         
  7.         
  8.         private Transform player;           // 玩家变换矩阵变量.
  9.         private Vector3 relCameraPos;       // 摄像机与玩家之间的相对位置向量.
  10.         private float relCameraPosMag;      // 摄像机到玩家之间的距离.
  11.         private Vector3 newPos;             // 摄像机将要到达的新位置.
  12.         
  13.         
  14.         void Awake ()
  15.         {
  16.                 // 初始化变量.
  17.                 player = GameObject.FindGameObjectWithTag(Tags.player).transform;
  18.                
  19.                 // 设置相对位置作为初始位置.
  20.                 relCameraPos = transform.position - player.position;
  21.                 relCameraPosMag = relCameraPos.magnitude - 0.5f;
  22.         }
  23.         
  24.         
  25.         void FixedUpdate ()
  26.         {
  27.                 // 设定一个标准位置从摄像机到玩家.
  28.                 Vector3 standardPos = player.position + relCameraPos;
  29.                
  30.                 // 摄像机在玩家头顶的高度.
  31.                 Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
  32.                
  33.                 // 创建一个数组,设定5个位置可以看到玩家.
  34.                 Vector3[] checkPoints = new Vector3[5];
  35.                
  36.                 // 第一个是摄像机的标准位置.
  37.                 checkPoints[0] = standardPos;
  38.                
  39.                 // 剩下三个分别是从悬浮位置到标准位置之间的几个位置.
  40.                 checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
  41.                 checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
  42.                 checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
  43.                
  44.                 // 最后一个是悬浮位置.
  45.                 checkPoints[4] = abovePos;
  46.                
  47.                 // 遍历这些点...
  48.                 for(int i = 0; i < checkPoints.Length; i++)
  49.                 {
  50.                         // ... 如果在当前位置可以看到玩家...
  51.                         if(ViewingPosCheck(checkPoints[i]))
  52.                                 // ... 返回.
  53.                                 break;
  54.                 }
  55.                
  56.                 // 从摄像机的当前位置过渡到新位置.
  57.                 transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
  58.                
  59.                 // 平滑过渡
  60.                 SmoothLookAt();
  61.         }
  62.         
  63.         
  64.         bool ViewingPosCheck (Vector3 checkPos)
  65.         {
  66.                 RaycastHit hit;
  67.                
  68.                 // 如果设想在看到玩家的图中碰到物体...
  69.                 if(Physics.Raycast(checkPos, player.position - checkPos, out hit, relCameraPosMag))
  70.                         // ... 这个物体不是玩家...
  71.                         if(hit.transform != player)
  72.                                 // 这个位置就不对.
  73.                                 return false;
  74.                
  75.                 // 如果中间没有任何阻挡.
  76.                 newPos = checkPos;
  77.                 return true;
  78.         }
  79.         
  80.         
  81.         void SmoothLookAt ()
  82.         {
  83.                 // 建立摄像机到玩家的射线.
  84.                 Vector3 relPlayerPosition = player.position - transform.position;
  85.                
  86.                 // Create a rotation based on the relative position of the player being the forward vector.
  87.                 // 获取旋转角
  88.                 Quaternion lookAtRotation = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
  89.                
  90.                 // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
  91.                 // 做插值
  92.                 transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
  93.         }
  94. }
复制代码
JS
  1. #pragma strict

  2. public var smooth : float = 1.5f;           // The relative speed at which the camera will catch up.


  3. private var player : Transform;             // Reference to the player's transform.
  4. private var relCameraPos : Vector3;         // The relative position of the camera from the player.
  5. private var relCameraPosMag : float;        // The distance of the camera from the player.
  6. private var newPos : Vector3;               // The position the camera is trying to reach.


  7. function Awake ()
  8. {
  9.     // Setting up the reference.
  10.     player = GameObject.FindGameObjectWithTag(Tags.player).transform;
  11.    
  12.     // Setting the relative position as the initial relative position of the camera in the scene.
  13.     relCameraPos = transform.position - player.position;
  14.     relCameraPosMag = relCameraPos.magnitude - 0.5f;
  15. }


  16. function FixedUpdate ()
  17. {
  18.     // The standard position of the camera is the relative position of the camera from the player.
  19.     var standardPos : Vector3 = player.position + relCameraPos;
  20.    
  21.     // The abovePos is directly above the player at the same distance as the standard position.
  22.     var abovePos : Vector3 = player.position + Vector3.up * relCameraPosMag;
  23.    
  24.     // An array of 5 points to check if the camera can see the player.
  25.     var checkPoints : Vector3[] = new Vector3[5];
  26.    
  27.     // The first is the standard position of the camera.
  28.     checkPoints[0] = standardPos;
  29.    
  30.     // The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
  31.     checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
  32.     checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
  33.     checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
  34.    
  35.     // The last is the abovePos.
  36.     checkPoints[4] = abovePos;
  37.    
  38.     // Run through the check points...
  39.     for(var i = 0; i < checkPoints.Length; i++)
  40.     {
  41.         // ... if the camera can see the player...
  42.         if(ViewingPosCheck(checkPoints[i]))
  43.             // ... break from the loop.
  44.             break;
  45.     }
  46.    
  47.     // Lerp the camera's position between it's current position and it's new position.
  48.     transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
  49.    
  50.     // Make sure the camera is looking at the player.
  51.     SmoothLookAt();
  52. }


  53. function ViewingPosCheck (checkPos : Vector3) : boolean
  54. {
  55.     var hit : RaycastHit;
  56.    
  57.     // If a raycast from the check position to the player hits something...
  58.     if(Physics.Raycast(checkPos, player.position - checkPos, hit, relCameraPosMag))
  59.         // ... if it is not the player...
  60.         if(hit.transform != player)
  61.             // This position isn't appropriate.
  62.             return false;
  63.    
  64.     // If we haven't hit anything or we've hit the player, this is an appropriate position.
  65.     newPos = checkPos;
  66.     return true;
  67. }


  68. function SmoothLookAt ()
  69. {
  70.     // Create a vector from the camera towards the player.
  71.     var relPlayerPosition : Vector3 = player.position - transform.position;
  72.    
  73.     // Create a rotation based on the relative position of the player being the forward vector.
  74.     var lookAtRotation : Quaternion = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
  75.    
  76.     // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
  77.     transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
  78. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-6-5 16:10

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表