unity新手可以参考,相机360度观察一个对象

来源:互联网 发布:pythonxy linux 编辑:程序博客网 时间:2024/06/08 13:18
Using UnityEngine;  
  
public class MouseFollowRotation : MonoBehaviour  
{  
    public Transform target;  
    public float xSpeed = 200;  
    public float ySpeed = 200;  
    public float mSpeed = 10;  
    public float yMinLimit = -50;  
    public float yMaxLimit = 50;  
    public float distance = 2;  
    public float minDistance = 2;  
    public float maxDistance = 30;  
  
    //bool needDamping = false;  
    public bool needDamping = true;  
    float damping = 5.0f;  
  
    public float x = 0.0f;  
    public float y = 0.0f;  
  
    // Use this for initialization  
    void Start()  
    {  
        Vector3 angles = transform.eulerAngles;  
        x = angles.y;  
        y = angles.x;  
    }  
  
    // Update is called once per frame  
    void LateUpdate()  
    {  
        if (target)  
        {  
            //use the light button of mouse to rotate the camera  
            if (Input.GetMouseButton(1))  
            {  
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;  
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;  
  
                y = ClampAngle(y, yMinLimit, yMaxLimit);  
            }  
            distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;  
            distance = Mathf.Clamp(distance, minDistance, maxDistance);  
            Quaternion rotation = Quaternion.Euler(y, x, 0.0f);  
            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);  
            Vector3 position = rotation * disVector + target.position;  
            //adjust the camera  
            if (needDamping)  
            {  
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);  
                transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);  
            }  
            else  
            {  
                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);  
    }  
}  
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大人的头偏了怎么办 宝宝脖子睡偏了怎么办 宝宝头歪向左边怎么办 一岁宝宝头歪怎么办 宝宝头往右边歪怎么办 八个月宝宝头歪怎么办 宝宝头往左边偏怎么办 11月婴儿歪脖子怎么办 婴儿头往左边偏怎么办 宝宝头网的高怎么办 宝宝头歪向一边怎么办 2岁宝宝头睡偏了怎么办 6岁儿童头有点歪怎么办 宝宝脖子有点偏左边歪怎么办 宝宝脖子偏了怎么办呢 斜颈导致的脸歪怎么办 斜颈手术后脸部还不对称怎么办 宝宝3个月斜颈怎么办 一岁八个月宝宝斜颈怎么办 四个月宝宝有点斜颈怎么办 一岁宝宝有点偏怎么办 6个月宝宝有点斜颈怎么办 四个月宝宝左侧胸锁乳突肌厚怎么办 脖子疼好几天了怎么办 有双下巴怎么办才能瘦掉 胃突然疼的厉害怎么办 手劳损痛的厉害怎么办 手臂扭到了很疼怎么办 寒湿导致肩膀痛怎么办 吃辣脖子肿了怎么办 脖子长了个肿瘤怎么办 一边背高一边低怎么办 生完孩子肩宽了怎么办 狗狗脖子上长囊肿怎么办 胸肋关节韧带疼怎么办 三岁宝宝有点驼背怎么办 20多岁有点驼背怎么办 五岁宝宝有点驼背怎么办 孕妇腰扭了很痛怎么办 怀孕了腰扭了疼怎么办 小龙芭比扭腰机开机没有声音怎么办