欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity3D实现物体旋转缩放移动效果

程序员文章站 2023-11-21 12:45:40
本文实例为大家分享了unity3d实现物体旋转缩放移动的具体代码,供大家参考,具体内容如下 由于项目运行在安卓上,运用到了插件,比较麻烦。你们可以在触发条件上进行修改,不...

本文实例为大家分享了unity3d实现物体旋转缩放移动的具体代码,供大家参考,具体内容如下

由于项目运行在安卓上,运用到了插件,比较麻烦。你们可以在触发条件上进行修改,不用插件也可以。

1.下载fingergestures 插件 下载地址 点击打开链接

2.导入插件,创建场景 将预设finger gestures initializer 拖拽到 hierarchy 视图中

3.添加脚本,拖拽到摄像机上。创建一个方块拖拽到脚本target 属性上。

using unityengine;
using system.collections;
 
public class objectcontrol : monobehaviour
{
 public transform target;
 public float yawsensitivity = 80.0f;
 public float pitchsensitivity = 160.0f;
 public bool clamppitchangle = true;
 public float pinchzoomsensitivity = 0.5f;//缩放速度
 public float smoothzoomspeed = 10.0f;
 public float smoothorbitspeed = 20.0f;
 public float distance = 0;
 
 float yaw = 0;
 float pitch = 0;
 float idealyaw = 0;
 float idealpitch = 0;
 float fchangescale = 0;
 float fchangeideal = 0;
 public transform[] movementp;
 
 /// <summary>
 /// 控制模式枚举
 /// </summary>
 public enum controlmodel
 {
  zoom, rotate, translate
 }
 
 public controlmodel controlmodel = controlmodel.rotate;
 
 //vector3 position=new vector3();
 public bool barrive = false;//鼠标是否到达零件箱边界区域
 //平移方式是否根据鼠标拖动距离还是直接置为鼠标位置
 public bool ifdragmove = false;
 //平移方式为:根据鼠标拖动距离 时,评议的速度
 public float movespeed = 1.0f;
 //是够需要画出按钮(缩放、旋转、平移)
 public bool ifdrawbtn = true;
 //缩放方式改为:改变相机范围
 public bool zoomcamera = false;
 //zoomcamera = true ,相机的最小范围值
 public float minzoom = 0f;
 //zoomcamera = true ,相机的最大范围值
 public float maxzoom = 179f;
 //平移对象
 public transform movetarget;
 //平移对象的初始位置
 vector3 movetargetpos;
 //模型的直接父对象
 public transform parentmodel;
 vector3 parentmodelpos;
 
 void start()
 {
  zoomcamera = true;
 }
 
 void onenable()
 {
 
  fingergestures.ondragmove += fingergestures_ondragmove;
  fingergestures.onpinchmove += fingergestures_onpinchmove;
  fingergestures.onfingerdragend += onfingerdragend;
 
 }
 
 void ondisable()
 {
  fingergestures.ondragmove -= fingergestures_ondragmove;
  fingergestures.onpinchmove -= fingergestures_onpinchmove;
  fingergestures.onfingerdragend -= onfingerdragend;
 }
 
 public void setrotation()
 {
  vector3 angles = target.eulerangles;
  yaw = idealyaw = angles.y;
  pitch = idealpitch = angles.x;
 }
 
 void fingergestures_ondragmove(vector2 fingerpos, vector2 delta)
 {
  ondrag = true;
  try
  {
   screen.showcursor = false;
  }
  catch
  {
   screen.showcursor = false;
  }
  if (controlmodel == controlmodel.rotate && !barrive)
  {
   idealyaw -= delta.x * yawsensitivity * 0.02f;
   idealpitch += delta.y * pitchsensitivity * 0.02f;
   len = delta;
   if (target) target.transform.rotate(new vector3(delta.y, -delta.x, 0), space.world);
  }
  if (controlmodel == controlmodel.translate && !barrive)
  {
   if (ifdragmove)
   {
    if (movetarget == null)
    {
     target.position = new vector3(target.position.x + delta.x * movespeed, target.position.y + delta.y * movespeed, target.localposition.z);// getworldpos( fingerpos );
    }
    else
    {
     movetarget.position = new vector3(movetarget.position.x + delta.x * movespeed, movetarget.position.y + delta.y * movespeed, movetarget.localposition.z);
    }
   }
   else
   {
    if (movetarget == null)
    {
     target.position = getworldpos(fingerpos);
    }
    else
    {
     movetarget.position = getworldpos(fingerpos);
    }
   }
  }
 
 }
 
 void fingergestures_onpinchmove(vector2 fingerpos1, vector2 fingerpos2, float delta)
 {
 
  if (controlmodel == controlmodel.zoom && !barrive)
  {
   if (zoomcamera)
   {
    float fzoom = camera.fieldofview - delta * pinchzoomsensitivity * 800 * time.deltatime;
    fzoom = mathf.min(fzoom, maxzoom);
    fzoom = mathf.max(fzoom, minzoom);
    camera.fieldofview = mathf.lerp(camera.fieldofview, fzoom, time.deltatime * smoothzoomspeed);
    // camera.transform.position = target.position - fzoom * camera.transform.forward;
   }
   else
   {
 
    fchangescale = target.localscale.x + delta * pinchzoomsensitivity;
 
    vector3 vc = new vector3(fchangescale, fchangescale, fchangescale);
   }
  }
 }
 //滑动结束
 void onfingerdragend(int fingerindex, vector2 fingerpos)
 {
  screen.showcursor = true;
 
  ondrag = false;
 }
 
 
 //把unity屏幕坐标换算成3d坐标
 vector3 getworldpos(vector2 screenpos)
 {
  // camera maincamera = camera.main;
  camera maincamera = gameobject.findgameobjectwithtag("maincamera").getcomponent<camera>();
  if (!maincamera.enabled)
  {
   maincamera = maincamera.transform.parent.findchild("cameraone").getcomponent<camera>();
  }
  return maincamera.screentoworldpoint(new vector3(screenpos.x, screenpos.y, mathf.abs(target.position.z - maincamera.transform.position.z)));
 }
 
 void apply()
 {
  if (controlmodel == controlmodel.rotate && !barrive)
  {
   yaw = mathf.lerp(yaw, idealyaw, time.deltatime * smoothorbitspeed);
   pitch = mathf.lerp(pitch, idealpitch, time.deltatime * smoothorbitspeed);
  }
 }
 bool ondrag;
 vector2 len;
 
 void lateupdate()
 {
  if (input.getmousebuttonup(1) || input.getmousebuttonup(0))
  {
   screen.showcursor = true;
  }
  apply();
 }
 
 static float clampangle(float angle, float min, float max)
 {
  if (angle < -360)
   angle += 360;
 
  if (angle > 360)
   angle -= 360;
 
  return mathf.clamp(angle, min, max);
 }
 
 void update()
 {
  ///*切换
 
  if (input.getmousebuttondown(0))
  {
 
   controlmodel = controlmodel.translate;
  }
 
  if (input.getmousebuttondown(1))
  {
 
   controlmodel = controlmodel.rotate;
  }
 
  if (input.getaxis("mouse scrollwheel") != 0)
  {
   controlmodel = controlmodel.zoom;
  }
 
 }
 
 /// <summary>
 /// 复位
 /// </summary>
 public void resetvalue()
 {
  if (movetarget != null)
  {
   movetarget.localposition = movetargetpos;
  }
  if (parentmodel != null)
  {
   parentmodel.localposition = parentmodelpos;
  }
  yaw = 0;
  pitch = 0;
  idealyaw = 0;
  idealpitch = 0;
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。