Unity场景内的个别物体无法用鼠标cad缩放物体

下次自动登录
现在的位置:
& 综合 & 正文
Unity鼠标中键滚动缩放相机,左键点击场景中模型居中显示
using UnityE
using System.C
public class MoustControl : MonoBehaviour
//鼠标右键可以拖拽旋转场景
//鼠标中键滚动拉伸镜头远近
private int MouseWheelSensitivity = 10;
private int maxCamFov = 90;
private int minCamFov = 10;
private Vector3
// Use this for initialization
void Start()
cam = Camera.main.
// Update is called once per frame
void Update()
if (Input.GetMouseButtonDown(0))
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
if (hit.transform.parent.name == "gobj")
print(hit.transform.name);
//点击当前对象,将主摄像机的镜头对准该模型,即该模型在摄像机镜头中间位置
//targetModel = GameObject.Find("GameObject").transform.FindChild(this.gameObject.name).gameO
Vector3 relativePos = hit.transform.position - cam.
print(relativePos.ToString());
Quaternion rotation = Quaternion.LookRotation(relativePos);
cam.rotation =
//cam.LookAt(hit.transform);
//滚动鼠标中键滚动缩放相机
if (Input.GetAxis("Mouse ScrollWheel") != 0)
float fov = Camera.main.fieldOfV
fov += Input.GetAxis("Mouse ScrollWheel") * MouseWheelS
fov = Mathf.Clamp(fov, minCamFov, maxCamFov);
Camera.main.fieldOfView =
【上篇】【下篇】程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
Unity3D研究院之鼠标拉近旋转拖拽模型(六十)
Unity3D研究院之鼠标拉近旋转拖拽模型(六十)
围观46599次
编辑日期: 字体:
今天帮一个朋友看了个虚拟现实的项目,他希望实现鼠标滚动拉近模型、鼠标右键旋转模型、鼠标中键拖拽模型,部分功能网上已经有答案、但是合在一起的没有找到,哪么我就加以总结把把功能都写在这个demo中吧。
把如下代码直接挂在摄像机对象上。最近心情很down,有点懒了就不写注释了,如果有朋友有疑问欢迎给我留言。。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
using UnityEngine;using System.Collections;&public class Script_07_11 : MonoBehaviour {& public Transform target; private int MouseWheelSensitivity = 1; private int MouseZoomMin = 1; private int MouseZoomMax = 5; private float normalDistance = 3;& private Vector3 normalized;& private float xSpeed = 250.0f; private float ySpeed = 120.0f;& private int yMinLimit = -20; private int yMaxLimit = 80;& private float x = 0.0f; private float y = 0.0f;& private Vector3 screenPoint; private Vector3 offset;& private Quaternion rotation = Quaternion.Euler(new Vector3(30f,0f,0f)); private Vector3 CameraTarget; void Start ()
CameraTarget = target.position;&
float z = target.transform.position.z - normalDistance;
transform.position =
rotation * new Vector3(transform.position.x,transform.position.y,z);&
transform.LookAt(target);&
var angles = transform.eulerAngles;&&&& x = angles.y;&&&& y = angles.x; }& void Update () {&
if(Input.GetMouseButton(1))
&&&&x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;&&&&&&&&
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;&
y = ClampAngle(y, yMinLimit, yMaxLimit);&&&&&&&&&
var rotation = Quaternion.Euler(y, x, 0);&&&&&&&&
var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;&&&&&&&&&
transform.rotation = rotation;&&&&&&&&
transform.position = position;&
}else if (Input.GetAxis("Mouse ScrollWheel") != 0)
&&&&normalized = (transform.position - CameraTarget).normalized;&
if (normalDistance &= MouseZoomMin && normalDistance &= MouseZoomMax)
normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
if (normalDistance & MouseZoomMin)
normalDistance = MouseZoomMin;
if (normalDistance & MouseZoomMax)
normalDistance = MouseZoomMax;
transform.position = && normalized * normalDistance;&
}else if(Input.GetMouseButtonDown(2))
{ &&&& screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);&&&&
offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
if(Input.GetMouseButton(2))
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);&
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
target.transform.position = curPosition;
transform.LookAt(CameraTarget);& }& 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); }}
最后是工程下载地址:
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!unity 场景中鼠标点击模型,相机瞬间定位于模型前方,怎么搞?大神求指教
[问题点数:30分]
unity 场景中鼠标点击模型,相机瞬间定位于模型前方,怎么搞?大神求指教
[问题点数:30分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|[Unity3d]控制物体的旋转以及缩放
我们在用unity做产品展示的功能的时候,旋转和缩放是必不可少的功能,如果是大型物品,比如汽车或者其他的东西,可以使用旋转摄像机的思路来控制达到物体旋转的效果,如果是这种情况可以参考我之前的文章:https://blog.csdn.net/dingxiaowei2013/article/details/;如果是小型物品的展示,就可以通过通过控制物体本身的旋转和位置的变化来达到缩放和旋转物体的效果。因为我这里有一个背景图,所以不希望背景图动,之所就只能采取第二种方法来达到缩放旋转物体的效果。
using UnityE
using System.C
public class DragModel : MonoBehaviour {
private float x = 0.0f;
private float y = 0.0f;
private float xSpeed = 200.0f;
private float ySpeed = 200.0f;
private float zSpeed = 40f;
private float normalDistence = 0;
private float minDistence = 0;
private float maxDistence = 10;
void Update ()
if(Input.GetMouseButton(1))
x=Input.GetAxis("Mouse X") *xS
print(Input.GetAxis("Mouse X"));
y=Input.GetAxis("Mouse Y") *yS
transform.Rotate(Vector3.up * -x *Time.deltaTime,Space.World);
transform.Rotate(Vector3.right * y *Time.deltaTime,Space.World);
else if(Input.GetAxis("Mouse ScrollWheel")!=0)
float ga = Input.GetAxis("Mouse ScrollWheel");
if(transform.position.z > minDistence && transform.position.z<maxDistence||transform.position.z<=minDistence && ga=maxDistence && ga>0)
transform.Translate(Vector3.forward*-Input.GetAxis("Mouse ScrollWheel")*zSpeed*Time.deltaTime,Space.World);原创文章如需转载请注明:转载自 &QQ群:【<span style="color: #192】本文链接地址:
Transform基本移动函数:
1.指定方向移动:
//移动速度
float TranslateSpeed = 10f;
//Vector3.forward 表示&向前&
transform.Translate(Vector3.forward *TranslateSpeed);
2.全方向移动:
//x轴移动速度移动速度
float xSpeed = -5f;
//z轴移动速度移动速度
zSpeed = 10f;
//向x轴移动xSpeed,同时想z轴移动zSpeed,y轴不动
transform.Translate(xSpeed,0,zSpeed);
3.重置坐标:
float xPostion = -5f;
float zPostion = 10f;
//直接将当前物体移动到x轴为xPostion,y轴为0,z轴为zPostion的三维空间位置。
transform.position = Vector3(xPostion,0,zPostion);
输入控制:
1.输入指定按键:
//按下键盘&上方向键&
if(Input.GetKey ("up"))
  print("Up!");
//按下键盘&W键&
if(Input.GetKey(KeyCode.W);)
  print("W!");
2.鼠标控制
//按下鼠标左键(0对应左键 , 1对应右键 , 2对应中键)
if(Input.GetMouseButton(0))
  print("Mouse Down!");Input.GetAxis("Mouse X");//鼠标横向增量(横向移动)&Input.GetAxis("Mouse Y");//鼠标纵向增量(纵向移动)
3.获取轴:
//水平轴/垂直轴 (控制器和键盘输入时此值范围在-1到1之间)
Input.GetAxis("Horizontal");//横向
Input.GetAxis ("Vertical");//纵向
按住鼠标拖动物体旋转和自定义角度旋转物体:
float speed = 100.0f;
void Update () {
  if(Input.GetMouseButton(0)){//鼠标按着左键移动
    y = Input.GetAxis("Mouse X") * Time.deltaTime *
    x = Input.GetAxis("Mouse Y") * Time.deltaTime *
  }else{
    x = y = 0 ;
  //旋转角度(增加)
  transform.Rotate(new Vector3(x,y,0));
  /**---------------其它旋转方式----------------**/
  //transform.Rotate(Vector3.up *Time.deltaTime * speed);//绕Y轴 旋转
  //用于平滑旋转至自定义目标
  pinghuaxuanzhuan();
//平滑旋转至自定义角度
void OnGUI(){
  if(GUI.Button(Rect(Screen.width - 110,10,100,50),"set Rotation")){
    //自定义角度
    targetRotation = Quaternion.Euler(45.0f,45.0f,45.0f);
    // 直接设置旋转角度
    //transform.rotation = targetR
    // 平滑旋转至目标角度
    iszhuan = true;
bool iszhuan= false;
Quaternion targetR
void pinghuaxuanzhuan(){
  if(iszhuan){
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
键盘控制物体缩放:
float speed = 5.0f;
void Update () {
x = Input.GetAxis("Horizontal") * Time.deltaTime *
z = Input.GetAxis("Vertical") * Time.deltaTime *
//垂直//"Fire1","Fine2","Fine3"映射到Ctrl,Alt,Cmd键和鼠标的三键或腰杆按钮。新的输入轴可以在Input Manager中添加。
transform.localScale += new Vector3(x, 0, z);
/**---------------重新设置角度(一步到位)----------------**/
//transform.localScale = new Vector3(x, 0, z);
阅读(...) 评论()}

我要回帖

更多关于 3d max 缩放物体 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信