俯瞰,点击鼠标右键360度旋转物体

using UnityEngine;
using System.Collections;


public class CustomMouseOrbit :MonoBehaviour
{
public struct CameraInitData{
public Vector3 CameraPos;
public Quaternion CameraRot;
public float Dis;
public float xx;
public float yy;
public Vector3 TargetPos;
public Quaternion TargetRot;
}
public enum OperateState {MANUAL,AUTO,NO_OPERATE};
public Transform target;
private Transform initTarget;
public float distance = 10.0f;
//MotionSpeed
public float MotionSpeed = 4.0f;
//Rotate  Speed
private float xSpeed = 250.0f;
private float ySpeed = 120.0f;
//Rotate Limit
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
//Limit value of rotation
public float xMinLimit = -360;
public float xMaxLimit = 360;
public float x = 0.0f;
public float y = 0f;
public float nearLimit = 0.8f;
public float farLimit = 5f;
public bool canManualRotate = true;
public bool canManualZoom = true;
public bool canManualMove = true;
//public Vector3 targetOffset;
//public float[] LimitsPos;
//Limit border
public float XBorderMin;
public float XBorderMax;
public float ZBorderMin;
public float ZBorderMax;
public float YBorderMin;
public float YBorderMax;
//mobile part var
Vector2[] touchInitPos=new Vector2[2];
Vector2[] touchNowPos = new Vector2[2];
float touchInitDis=0;
float touchNowDis = 0;
//bool 
public   bool onlyX = false;
    private string strOrgHitName;
    public float  MobileRotSpeed = 10.0f;
//Keep touch values
private Touch touch1,touch2,touch3;
private float touchDistance,lastTouchDistance,deltaDistance;
private Vector2 touchDeltaPosition;
public float movSpeed = 0.005f;
public float movSpeedScroll = 15f;
//Rotate center position
private Vector3 vCenter;//For another script to call it ,change its type as public
public  Vector3 OriginPos;
private bool bMove = true;
private  float fFieldofView;
    //private float distance = 5.0f;//the distance from camera to target
//public float zoomRate = 1.0f;
private float fOrgFieldView;
private Vector3 vCamPos;
private Quaternion qCamQua;
//target original position
private Vector3 vTagPos;
private Quaternion qTagQua;
private  string strHitObj;//The  touch name
//Use this for initialization
float yDis,xDis;
private bool bFstTouch;
private Vector3 vOrgPos ;
private Vector3 vCurPos ;
Quaternion rotation;
Vector3 position;


    



public int UILayerIndex;
public Camera[] UIcameras;

private CameraInitData _CamData;
private bool IsStart = false;
//RoateAround
private OperateState _currentOperateState = OperateState.MANUAL;
public OperateState CurrentOperateState
{
get{return _currentOperateState;}
set{_currentOperateState = value;}
}
private float _oldX;
private float _oldY;
private float _oldDis;
private Vector3 _oldPos;
private float _rotSpeed = 0;
private float _TargetX = 0;
//private Transform _currentTarget;
//

    void Start()
    {
  
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
        initTarget = target;

vCenter = target.position;
   rotation = Quaternion.Euler(y,x,0);
position = rotation*new Vector3(0.0f,0.0f,-distance) + vCenter;
transform.rotation = rotation;
transform.position = position;
//Get original camera parameter
// fFieldofView = Camera.main.fieldOfView;
fOrgFieldView = fFieldofView;
vCamPos = transform.position;
qCamQua = transform.rotation;
//Get target original transform parameters
vTagPos = target.position;
qTagQua = target.rotation;
//oridistance=distance;
SaveInitData();
IsStart = true;
    }
void SaveInitData()
{
_CamData = new CameraInitData();
_CamData.CameraPos = this.gameObject.transform.localPosition;
_CamData.CameraRot = this.gameObject.transform.localRotation;
_CamData.Dis = distance;
_CamData.xx = x;
_CamData.yy = y;
_CamData.TargetPos = target.transform.localPosition;
_CamData.TargetRot = target.transform.localRotation;
}
void OnEnable()
{
if(!IsStart)
{
Start();
}
}
    void LateUpdate()
    {

if (target && _currentOperateState == OperateState.MANUAL)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer ||Application.platform == RuntimePlatform.WindowsWebPlayer||Application.platform == RuntimePlatform.OSXEditor)
{
//旋转
if (Input.GetMouseButton(1) && canManualRotate)
{
if(SelectUI(Input.mousePosition))
{
return;
}
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
x = ClampAngle(x,xMinLimit,xMaxLimit);
if (onlyX == false)
{
y -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
}
//鼠标中键
if(canManualMove)
{
if(Input.GetMouseButton(2))
{
vCurPos = Input.mousePosition;
if(!bFstTouch)
{
vOrgPos = vCurPos;
bFstTouch = true;
return;
}
if(vOrgPos != vCurPos)
{   
if(transform.position.y>7f)
{
yDis = (vCurPos.y - vOrgPos.y)*movSpeed*2f;
xDis = (vCurPos.x - vOrgPos.x)*movSpeed*2f;
vOrgPos = vCurPos;//Record last mouseposition
//Change target's position,acording' its rotation and the distance mouse moved
Quaternion rot = Quaternion.Euler(y,x,0);
Vector3 pos = rot*new Vector3(-xDis,-yDis,0) + target.position;
target.position = pos;
vCenter = target.position;


}
}
}
}
if(Input.GetMouseButtonUp(2))
{
bFstTouch = false;
}
//镜头远近
if (Input.GetAxis("Mouse ScrollWheel") != 0 && canManualZoom)
{
distance -= Input.GetAxis("Mouse ScrollWheel") * movSpeedScroll;
distance = Mathf.Clamp(distance, nearLimit, farLimit);
}

            }
            else
            { 

int iTouchCount = Input.touchCount;
RaycastHit hit;
switch (iTouchCount)
{
case 1:
//First check whether it is select center operation,then execute rotate operation,including get hitspot's tag
touch1 = Input.GetTouch(0);
if(SelectUI(touch1.position))
{
return;
}
if(touch1.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(touch1.position);
if(Physics.Raycast(ray,out hit))
{
strHitObj = hit.transform.gameObject.tag;
}
}
//Execute rotation operation
if(touch1.phase == TouchPhase.Moved)
{
x += (float)((Input.GetTouch(0).deltaPosition.x)*MobileRotSpeed *0.03f);
//x += Input.GetAxis("Mouse X") * MobileRotSpeed * 0.03f;    
x = ClampAngle(x,xMinLimit,xMaxLimit);
y -= (float)((Input.GetTouch(0).deltaPosition.y) *MobileRotSpeed *0.03f);
//y -= Input.GetAxis("Mouse Y") * MobileRotSpeed * 0.03f; 
y = ClampAngle(y,yMinLimit,yMaxLimit);




}
    
break;
   case 2:
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
if(touch1.phase==TouchPhase.Moved||touch2.phase==TouchPhase.Moved)   
       {

        touchDistance = (touch1.position - touch2.position).magnitude;
lastTouchDistance = ((touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition)).magnitude;
deltaDistance =  lastTouchDistance - touchDistance;



distance += deltaDistance * movSpeedScroll;    

distance = Mathf.Clamp(distance, nearLimit, farLimit);
         
       }  

break;
case 3:
if(canManualMove)
{
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
touch3 = Input.GetTouch(2);
if(touch1.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved && touch3.phase == TouchPhase.Moved)
{
                      vCurPos=touch3.position;
if(!bFstTouch)
{
vOrgPos = vCurPos;
bFstTouch = true;
return;
}
if(vOrgPos != vCurPos)

yDis = (vCurPos.y - vOrgPos.y)*movSpeed;
xDis = (vCurPos.x - vOrgPos.x)*movSpeed;
vOrgPos = vCurPos;//Record last mouseposition
//Change target's position,acording' its rotation and the distance mouse moved
Quaternion rot = Quaternion.Euler(y,x,0);
Vector3 pos = rot*new Vector3(-xDis,-yDis,0)+target.position;
target.position = pos;
}
}
}
break;
case 0:
bFstTouch = false;
break;
}
}
//JudgeBorder(target.position);
y = ClampAngle(y, yMinLimit, yMaxLimit); 
            rotation = Quaternion.Euler(y, x, 0);
position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position/*+targetOffset*/;
transform.position= Vector3.Lerp(transform.position, position, Time.deltaTime * MotionSpeed);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * MotionSpeed);

       }else if(_currentOperateState == OperateState.AUTO)
{
x+=_rotSpeed;

rotation = Quaternion.Euler(y, x, 0);
position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position/*+targetOffset*/;
transform.position= Vector3.Lerp(transform.position, position, Time.deltaTime * MotionSpeed);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * MotionSpeed);
if(Mathf.Abs(_TargetX -x) <1)
{

//distance = _currentDis;
//target.position = _oldPos;
//x = _oldX;
//y = _oldY;

//_currentOperateState = OperateState.MANUAL;
//SetTargetPos(target.position,_oldDis);
//TweenPosition.Begin(gameObject,0.8f,_currentCamPos);
//TweenRotation.Begin(gameObject,0.8f,_currentCamRot);
//Invoke("ReturnToManual",0.3f);
ReturnToManualOperate();
}
}
    }
public void ReturnToManualOperate()
{
target.position = _oldPos;
x = _oldX;
y = _oldY;

_currentOperateState = OperateState.MANUAL;
SetTargetPos(target.position,_oldDis);
}
public void Reset()
{
//reset camera's parameters
Camera.main.fieldOfView = fOrgFieldView;
   Camera.main.transform.position = vCamPos;
transform.rotation = qCamQua;
   target.position=vCenter;
//x = 90.0f;
//y = 11.0f;
Quaternion rotation = Quaternion.Euler(y,x,0);
transform.rotation = rotation;
Vector3 position = rotation*new Vector3(0.0f,0.0f,-distance) + vCenter;
//distance=40f;
distance=300f;
transform.rotation = rotation;//Initialize camera's rotation
transform.position = position;//Initialize camera's position
}
public void ResetViewToInit()
{
this.gameObject.transform.localPosition = _CamData.CameraPos;
this.gameObject.transform.localRotation = _CamData.CameraRot;
this.distance = _CamData.Dis;
this.x = _CamData.xx;
this.y = _CamData.yy;
this.target.transform.localPosition = _CamData.TargetPos;
this.target.transform.localRotation = _CamData.TargetRot;
}
public void SetObjectToCenter(GameObject obj,float disval,float xval,float yval)
{
SetCameraAngle(xval,yval);
SetCameraDistance(disval);
target.transform.position=obj.transform.position;

Quaternion rotation = Quaternion.Euler(yval,xval,0);
transform.rotation = rotation;
Vector3 position = rotation*new Vector3(0.0f,0.0f,-distance) + obj.transform.position;
TweenRotation.Begin(this.gameObject,0.3f,rotation);
// transform.rotation = rotation;//Initialize camera's rotation
//transform.position = position;//Initialize camera's position
TweenPosition.Begin(this.gameObject,0.3f,position);
}

public void ResetMyNiaoKanCam()
{
SetCameraAngle(84.5f,16.15f);
SetCameraDistance(310f);
target.transform.position=OriginPos;

}
public void ResetMySheBeiBuJuCam()
{
SetCameraAngle(180f,69.5f);
SetCameraDistance(230.2f);
target.transform.position=OriginPos;

}
    public void SetTargetPosYOffset(float t)
    {
        target.position = transform.position + transform.TransformDirection(0, t, 0);
    }
public  void  CameraTargrtOrgin()
{
Reset();
target=initTarget;
}

    public void CameraAutoRotate()
    {
        if (canManualRotate)
        {
            x +=xSpeed * Time.deltaTime;
            onlyX = true;
        }
    }
public void RotateAroundTarget(Transform Target,float dis =30,float speed = 0.2f)
{
_oldX = x;
_oldY = y;
_oldDis = distance;
_oldPos = target.position;
target.position = Target.position;
//distance = dis;
_TargetX = x+360;
_rotSpeed = speed;
SetTargetPos(target.position,dis);
_currentOperateState = OperateState.AUTO;
}
   public  void ResetCameraAngle()
    {
        y = 20;
        x = 0;
    }


   public  void SetCameraNearLimit(float near)
    {
        nearLimit = near;
    }


   public  void SetManualRotate(bool rt)
    { 
canManualRotate = rt;
}
   public  void SetCameraTarget(GameObject obj)
    {
        target = obj.transform;
    }
public void SetTargetPos(Vector3 pos,float dis = 10.0f)
{
distance = dis;
target.transform.localPosition = pos;
}
    public void SetCameraBuZhouMode()
    {
        nearLimit = 0.8f; farLimit = 1.8f;
    }
    public void SetCameraGuiWei()
    {
        SetCameraDistance(17f);
        SetCameraAngle(197.3f, 14.9f);
        SetCameraNearLimit(10);

//targetOffset=new Vector3 (0f,5.8f,0f);
        farLimit = 300f;
    }
    public void SetCameraanimatiZhiTui01()
    {
        SetCameraDistance(17f);
        SetCameraAngle(197.3f, 14.9f);
        SetCameraNearLimit(4);
        farLimit = 30f;
    }
    public void SetCameraMBallMode()
    {
        distance = 1.8f;
        nearLimit = 1.8f; farLimit = 1.8f;
        target = initTarget;
    }


    public void SetCameraAngle(float rx, float ry)
    {
        x = rx; y = ry;
    }


    void SetTargetPosXOffset()
    {
        target.position = transform.position + transform.TransformDirection(0,0,1.8f);
    }


    void SetCameraHAngle(float ang)
    {
        y = ang;
    }
    void SetCameraVAngle(float ang)
    {
        x = ang;
    }


   public  void SetCameraDistance(float dis)
    {
        distance = dis;
    }



    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);
    }




    public void ZoomCamera(float d,bool zoomin)
    {
        if (zoomin)
        { distance -= d * Time.deltaTime; }
        else
        { distance += d * Time.deltaTime; }
        
        distance = Mathf.Clamp(distance, nearLimit, farLimit);
    }


    public void RotateCamera(float d, bool left)
    {
        if (left)
        {
            x += d * Time.deltaTime;
        }
        else
        {
            x -= d * Time.deltaTime;
        }
    }
public bool SelectUI(Vector3 pos)
{
//pos = Camera.main.WorldToScreenPoint(pos);
foreach(Camera cam in UIcameras)
{
Ray ray = cam.ScreenPointToRay(pos);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
GameObject obj = hit.collider.gameObject;
if(obj.layer == UILayerIndex)
{


return true;
}
}
}
return false;
}
public bool isEnlarge(Vector2 oP1 ,Vector2 oP2 ,Vector2 nP1 ,Vector2 nP2 )
   {   
   //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势   
  float leng1 =Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));   
   float  leng2 =Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));   
   if(leng1<leng2)   
   {   
        //放大手势   
        return true;    
   }else   
   {   
       //缩小手势   
       return false;    
   }   
   }   
void JudgeBorder(Vector3 pos)
{
if(pos.x >=XBorderMin && pos.x <=XBorderMax && pos.z <=ZBorderMax && pos.z >=ZBorderMin&&pos.y <= YBorderMax && pos.y >=YBorderMin)
{

}else
{
Vector3 nowPos = new Vector3(0,0,0);
float xx = pos.x;
float yy = pos.y;
float zz = pos.z;
if(pos.x <XBorderMin)
{
xx = XBorderMin;
}
else if(pos.x >XBorderMax)
{
xx = XBorderMax;
}
if(pos.z <ZBorderMin)
{
zz = ZBorderMin;
}
else if(pos.z >ZBorderMax)
{
zz = ZBorderMax;
}
if(pos.y >YBorderMax)
{
yy = YBorderMax;
}
else if(pos.y <YBorderMin)
{
yy = YBorderMin;
}
TweenPosition.Begin(target.gameObject,0.1f,new Vector3(xx,yy,zz));

}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值