MouseOrbit

来源:互联网 发布:php加密方式 编辑:程序博客网 时间:2024/05/17 23:03
using UnityEngine;using System.Collections;public class MouseOrbit : MonoBehaviour{    public Transform target;    float distance = 30.0f;    float xSpeed = 250.0f;    float ySpeed = 120.0f;    float yMinLimit = -20;    float yMaxLimit = 80;    private float x = 0.0f;    private float y = 0.0f;    void Start()    {        var angles = transform.eulerAngles;        x = angles.y;        y = angles.x;        // Make the rigid body not change rotation        if (GetComponent<Rigidbody>())        {            GetComponent<Rigidbody>().freezeRotation = true;        }    }    void LateUpdate()    {        if (Input.GetMouseButton(0))        {            if (target)            {                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, -distance) + target.position;                transform.rotation = rotation;                transform.position = position;            }        }    }    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);    }}

0 0
原创粉丝点击