神仙客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using UnityEngine;
using System.Collections;

public class MouseEvent : MonoBehaviour {
    
    public Transform target;
    
    private float xSpeed = 250.0f;
    //private float ySpeed = 120.0f;
    private float speed=20.0F;  
    private float x = 0.0f;
    private float y = 0.0f; 
    private Quaternion mRotation;      
    public float Damping=10F;    
    private Vector3 offset;
    private Vector3 mousePosition;

    private float distance = 10.0f;
    private float height = 5.0f;
    private float rotationDamping;
    private float heightDamping;


    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton (0)) {
            //
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit)) {
                
                
                //旋转
                x -= Input.GetAxis("Mouse X") * xSpeed * 0.02F;   
                y =target.position.y; // Input.GetAxis("Mouse Y")* ySpeed * 0.02F;  //transform.position.y; // 
                
                
                //手机触摸情况
                //x -= Input.GetTouch (0).deltaPosition.x * xSpeed * 0.02F;   
                //y = Input.GetTouch (0).deltaPosition.y * ySpeed * 0.02F;             
                //y = ClampAngle(y,xSpeed,ySpeed);   
                
                mRotation = Quaternion.Euler (y, x, 0);      
                target.rotation = Quaternion.Lerp (target.rotation, mRotation, Time.deltaTime * Damping); 
                
                
            }
            
        }else if (Input.GetAxis ("Mouse ScrollWheel") != 0) {
            //Zoom out  
            if (Input.GetAxis ("Mouse ScrollWheel") < 0) {  
                if (Camera.main.fieldOfView <= 100)  
                    Camera.main.fieldOfView += 2;  
                if (Camera.main.orthographicSize <= 20)  
                    Camera.main.orthographicSize += 0.5F;  
            }
            //Zoom in  
            if (Input.GetAxis ("Mouse ScrollWheel") > 0) {  
                if (Camera.main.fieldOfView > 2)  
                    Camera.main.fieldOfView -= 2;  
                if (Camera.main.orthographicSize >= 1)  
                    Camera.main.orthographicSize -= 0.5F;  
            }  
            
        } 
        
        
        if (Input.GetMouseButton (2)) {
            /*
            Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);
                
            Vector3 offset = transform.position -Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, screenSpace.y, screenSpace.z));
            //Vector3 offset = transform.position -Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).deltaPosition.x, screenSpace.y, screenSpace.z));

            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, screenSpace.y, screenSpace.z);
            //Vector3 curScreenSpace = new Vector3(Input.GetTouch(0).deltaPosition.x, screenSpace.y, screenSpace.z);
            Vector3 ss=transform.position+offset;  
            transform.position = ss;   
             */
            target.position -= Vector3.right * Time.deltaTime*Input.GetAxis ("Mouse X") * speed;
            target.position += Vector3.up * Time.deltaTime*Input.GetAxis ("Mouse Y")* speed;    
            
        }
    }



    void OnGUI ()
    {
        Event Mouse = Event.current;
        //鼠标双击事件
        if (Mouse.isMouse && Mouse.type == EventType.MouseDown && Mouse.clickCount == 2)
        {
            //target.position = new Vector3(Input.GetAxis ("Mouse X")+10.0f,target.position.y,target.position.z+10.0f);
            // Calculate the current rotation angles
            var wantedRotationAngle = target.eulerAngles.y;
            var wantedHeight = target.position.y + height;
            
            var currentRotationAngle = transform.eulerAngles.y;
            var currentHeight = transform.position.y;
            
            // Damp the rotation around the y-axis
            currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
            
            // Damp the height
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
            
            // Convert the angle into a rotation
            var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
            
            // Set the position of the camera on the x-z plane to:
            // distance meters behind the target
            transform.position = target.position;
            transform.position -= currentRotation * Vector3.forward * distance;
            
            // Set the height of the camera
            transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
            
            // Always look at the target
            transform.LookAt(target);

        }
    }


    #region ClampAngle
    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);
    }
    #endregion
}

 

posted on 2015-06-29 16:26  神仙客  阅读(4603)  评论(0编辑  收藏  举报