usingUnityEngine;
usingSystem.Collections;
/**
* @Function:使物體自動(dòng)旋轉(zhuǎn)队寇,鼠標(biāo)能夠360°拖轉(zhuǎn)物體尝江,托轉(zhuǎn)物體的時(shí)候,自動(dòng)旋轉(zhuǎn)停止英上,同時(shí)滾輪實(shí)現(xiàn)物體的縮放功能
* @Ahthor:黃杰
* @Date:2013-04-24
*/
publicclassZoomAndDrag : MonoBehaviour {
publicCamera MainCamera;
publicfloatZoomMin;//滾輪的最小值
publicfloatZoomMax;//滾輪的最大值
privatefloatnormalDistance;//設(shè)置攝像機(jī)的景深值
privatefloatMouseWheelSencitivity = 10.0f;//鼠標(biāo)靈敏度,就是縮放的速度的快慢
privatefloataxisX;
privatefloataxisY;
publicfloatspeed = 6f;
privatefloattempSpeed;
privateboolRoationOnly;
voidStart ()
{
normalDistance = 50.0f;
ZoomMin = 20.0f;
ZoomMax = 100.0f;
RoationOnly =true;
}
voidUpdate ()
{
Roation();
Zoom();
this.transform.Rotate(newVector3(axisY, axisX, 0) * Rigid(), Space.World);//物體旋轉(zhuǎn)的方法
}
//自動(dòng)旋轉(zhuǎn)物體的方法,放在Update中調(diào)用
voidRoation()
{
if(RoationOnly)
{
gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 10);
}
}
/****
*鼠標(biāo)滾輪縮放物體的方法
*
* **/
voidZoom()
{
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
if(normalDistance >= ZoomMin && normalDistance <= ZoomMax)
{
normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity;
}
if(normalDistance < ZoomMin)
{
normalDistance = ZoomMin;
}
if(normalDistance > ZoomMax)
{
normalDistance = ZoomMax;
}
MainCamera.fieldOfView = normalDistance;
}
}
/***
*
* 鼠標(biāo)左鍵控制物體360°旋轉(zhuǎn)+慣性
* **/
floatRigid()
{
if(Input.GetMouseButton(0))
{
RoationOnly =false;//當(dāng)鼠標(biāo)按下的時(shí)候啤覆,停止自動(dòng)旋轉(zhuǎn)
axisX = Input.GetAxis("Mouse X");
axisY = Input.GetAxis("Mouse Y");
if(tempSpeed < speed)
{
tempSpeed += speed * Time.deltaTime * 5;
}
else
{
tempSpeed = speed;
}
}
else
{
RoationOnly =true;//當(dāng)鼠標(biāo)沒(méi)有按下的時(shí)候苍日,恢復(fù)自動(dòng)旋轉(zhuǎn)
if(tempSpeed > 0)
{
tempSpeed -= speed * Time.deltaTime;
}
else
{
tempSpeed = 0;
}
}
returntempSpeed;
}
}