Unity通过相机控制场景模型的移动和缩放

本文介绍如何使用Unity5.4.2f2及UGUI创建一个可实时调整播放模型位置与大小的场景。通过Canvas、Image、Camera和RawImage组件搭建基本界面,并利用脚本控制模型的上下左右移动及缩放效果。

Unity5.4.2f2、UGUI
1.创建一个Canvas。创建一个Image,用于放置背景图。
2.Image下面创建一个Camera,一个Render Texture,把这个Render Texture拖至Camera的Target Texture。
3.Image下面创建一个Raw Image,把Render Texture拖至Raw Image上。

层级结构:
Canvas
->Image

  • ->Raw Image
  • ->Camera

需求:播放的模型能够上下左右移动,放大缩小

左右移动
mCamera.transform.RotateAround(mRotatePoint.position,mCamera.transform.up,mRightSpeed * Input.GetAxis("Mouse X");

上下移动
mCamera.transform.RotateAround(mRotatePoint,position,mCamera.transform.right,mUpSpeed * Input.GetAxis("Mouse Y");
放大缩小
        //里滑
        if(Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if(mAnimationCamera.GetComponent<Camera>().fieldOfView <=100)
            {
                mAnimationCamera.GetComponent<Camera>().fieldOfView += 2;
            }
            if(mAnimationCamera.GetComponent<Camera>().orthographicSize <= 20)
            {
                mAnimationCamera.GetComponent<Camera>().orthographicSize += 0.5f;
            }
        }

        //外滑
        if(Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (mAnimationCamera.GetComponent<Camera>().fieldOfView > 25)
            {
                mAnimationCamera.GetComponent<Camera>().fieldOfView -= 2;
            }
            if (mAnimationCamera.GetComponent<Camera>().orthographicSize >= 1)
            {
                mAnimationCamera.GetComponent<Camera>().orthographicSize -= 0.5f;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值