旋转camera到特定对象

设定一个物体使得camera可以从现在为止自动飞到当前位置

 1. 设定一个位置,可以在其前方放置一个显示其位置的cube。这里也可以写脚本设定位置。

 2. 使用函数

  移动函数 transform.position = vector3.slerp(positionstart, positionEnd, Time*time*0.001);

  旋转函数 transform.Rotation = Quaternion.slerp(StartRotation, aimRotation, Time.time*0.00001);

 3.在控制自动漫游于原始FPC摄像机的切换脚本中添加功能:按下run,自动记录当前position,rotation;按FPS,恢复原来位置;

 4. aimRotation制定:

   4.1 由于我的摄像机一开始旋转了180度,因此需要添加

        aimRotation = Inverse(CubeRotation);

        aimRotation.y = 180度 + aimRotation.y;

   4.2 当移动到aimpostion时,FPS在轻微移动,因此加上判断;FPS与aimPostion 很近的时候,就直接将位置赋值给FPS;

Time

     Time.time 这一帧执行的时间距离开始时间

     Time.deltatime * 10 每帧移动10米

   这段脚本是我写的包含控制函数使得摄像机首先扭转方向到目标物体,然后按照该方向飞向目标物体,包含当物体里目标位置很近的时候,模糊摄像机,然后恢复正常状态

var startObject : GameObject;
var aimObject : GameObject;
var aimObjectLocation : GameObject;
var maincamera : Camera;
private var PositionStart : Vector3;
private var PositionEnd : Vector3;
private var aimRotation : Quaternion;
private var startRotation : Quaternion;
private var distance : float = 10;
private var angle : float = 10;
private var isBlur : boolean = false;
private var isCloseEnough : boolean = false;
private var relativePos : Vector3;
private var rotationFinished : boolean = false;
private var angleDefinition : boolean = false;
private var translateBegin : boolean = false;

function Update () {
   PositionStart = startObject.transform.position;
   PositionEnd = aimObjectLocation.transform.position;
   if(!translateBegin)
   {    
        relativePos = aimObject.transform.position - PositionStart;
        if(transform.position == PositionEnd)
        {
             rotationFinished = true;
        }
        else
        {
            if(relativePos != Vector3.zero)
            {
                 aimRotation = Quaternion.LookRotation(relativePos);
            }
            else
           {
                rotationFinished = true;
           }
           var angle = Quaternion.Angle(transform.rotation, aimRotation); 
           if (angle != 0)
          {
               rotationFinished = false;
               //distance = 10;
          }
          else
          {
               rotationFinished = true;
          }
       }
   }
   if(!rotationFinished)
   {
       if(angle > 0.1)
       {
           print("rotationlast");
           transform.rotation = Quaternion.Slerp(startObject.transform.rotation,
              aimRotation, 0.15);//normal velocity
       }
       else
       {
           print("rotationTheSame");
           rotationFinished = true;
           transform.rotation = aimRotation;
           translateBegin = true;
       }
   }
    
   if(translateBegin)
   {
       distance = Vector3.Distance(transform.position, PositionEnd);
       print(distance);
       if(distance < 0.1)
       {
           print("distance< 0.1");
           transform.position = PositionEnd;
           translateBegin = false;
   
           WaitToBlur(); 
           //maincamera.orthographic = true;
           transform.rotation = aimObjectLocation.transform.rotation;
       }
       else
       {
           transform.LookAt(aimObject.transform);
           transform.position = Vector3.Slerp(PositionStart,PositionEnd,
                                         0.08);
       }
    }
}

function WaitToBlur() {
    // suspend execution for waitTime seconds
    var blurCamera= maincamera.GetComponent("BlurEffect");
    blurCamera.enabled = true;
    yield WaitForSeconds (0.5);
    blurCamera.enabled = false;
}


posted @ 2010-09-30 13:28  子非あ鱼  阅读(1544)  评论(0编辑  收藏  举报