纳金网

标题: 物体的缩放 [打印本页]

作者: MK_New4    时间: 2015-3-2 13:02
标题: 物体的缩放
缩放手势是用两个手指来模拟的,当两手之间的距离越来越大,那就是放大,反之缩小;
    放大的方法有两种:
       1.物体用localScale来放大物体。不知道为什么我用这种放大得不到我想要的效果;
       2.拉近摄像机,这里面可就有一点学问了,我记得我在看OpenGL的时候,上面用铁轨来比喻一个视觉效果,实际当中铁轨是平行的不相交,但是我们视觉上的效果就是铁轨间的距离越来越近,最后相交了。unity3D默认的是相交的效果(camera-->Projection-->perspective),当然也有不想交的效果了(camera-->
Projection-->orthographic),你们试试效果很不一样的。如果你想加入天空盒而且不想随着物体缩放,看到天空背景也移动的话,那就选第二种。我加了天空盒,所以用第二种了,呵呵~~~
[java]
<p>
</p><pre name="code" class="java" style="background-color: rgb(255, 255, 255); "><pre name="code" class="java">var rotatepos:Transform;//rotatepos是该物体的一个子物体,放到物体的中心地方就行。
var horizontalSpeed : float ;
var verticalSpeed : float ;
var flag:boolean;

var olddis:float=0;
var newdis:float=0;
var child:Transform;


function Start(){

  horizontalSpeed=0;

  verticalSpeed=0;

  flag=true;

}



function Update () {
if(flag){
  transform.RotateAround(rotatepos.position,Vector3.up,1);
}
//物体的旋转
if(Input.touchCount==1){
   
  if(Input.GetTouch(0).phase==TouchPhase.Moved){
   flag=false;
   var h : float=Input.GetAxis("Mouse X");//又正左负
   var v : float =Input.GetAxis("Mouse Y");//上正下负

   

if(Mathf.Abs(h)>=Mathf.Abs(v)){  
   if(h<0){
     horizontalSpeed=6;
     transform.RotateAround(rotatepos.position,Vector3.up,horizontalSpeed);
   }
   if(h>0){
     horizontalSpeed=6;
     transform.RotateAround(rotatepos.position,-Vector3.up,horizontalSpeed);
   }

  }
   

  else{
   if(v<0){
     verticalSpeed=6;
     transform.RotateAround(rotatepos.position,-Vector3.right,verticalSpeed);
   }
   if(v>0){
     verticalSpeed=6;
     transform.RotateAround(rotatepos.position,Vector3.right,verticalSpeed);
   }
  }

   

}
}

  

  
  //物体的缩放
if(Input.touchCount>1){

    flag=false;
   if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved){

   
    var pos1=Input.GetTouch(0).position;
    var pos2=Input.GetTouch(1).position;   
    newdis=Vector2.Distance(pos1,pos2);
    if(olddis!=null){  
      if(newdis<olddis) {         
        Camera.main.camera.orthographicSize+=2;   
      }
      if(newdis>olddis) {
        Camera.main.camera.orthographicSize-=2;  
      }      
    }
    olddis=newdis;   

   }     
}   
   
   
   //按返回键退出程序
   if(Input.GetKey(KeyCode.Escape)){
      Application.Quit();
   }
}






欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5