码迷,mamicode.com
首页 > 编程语言 > 详细

[Unity实战]相机观察物体

时间:2015-04-04 16:49:54      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:相机

在原有的旋转、缩放的基础上加上了“惯性”。


using UnityEngine;
using System.Collections;

public class CameraObserve : MonoBehaviour {

    public Transform targetObj;
    public float rotateSpeed = 100f;
    public float translateSpeed = 10f;

    public float minDistance = 3f;//缩放观察的最小距离
    public float maxDistance = 15f;//缩放观察的最大距离

    public bool isInertiaRot = false;//松开左键后是否会继续旋转少量角度
    public bool isInertiaTra = false;//不滚滚轮后是否会继续前进少量距离

    private float mouseXRecord;
    private float mouseScrollRecord;

	// Update is called once per frame
	void Update () 
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseScroll = Input.GetAxis("Mouse ScrollWheel");

        if (Input.GetMouseButton(0))
        {
            if (mouseX != 0)
            {
                transform.RotateAround(targetObj.position, targetObj.up, mouseX * rotateSpeed * Time.deltaTime);
                mouseXRecord = mouseX;
            }
        }

        if (mouseScroll != 0)
        {
            //print(Vector3.Distance(targetObj.position,transform.position));
            Vector3 a = transform.position + (targetObj.position - transform.position) * mouseScroll * translateSpeed * Time.deltaTime;
            float distance = Vector3.Distance(targetObj.position, a);

            if ((distance > minDistance) && (distance < maxDistance))
                transform.position = a;

            mouseScrollRecord = mouseScroll;
        }         
	}

    void LateUpdate()
    {
        if (isInertiaRot && Input.GetMouseButtonUp(0))
        {
            InvokeRepeating("Rotate",0,Time.deltaTime);
            StartCoroutine(StopRotate(0.5f));
        }

        if (isInertiaTra && (Input.GetAxis("Mouse ScrollWheel") == 0) && (mouseScrollRecord != 0))
        {
            InvokeRepeating("Translate", 0, Time.deltaTime);
            StartCoroutine(StopTranslate(2f));
            isInertiaTra = false;
        }
    }

    void Rotate()
    {
        transform.RotateAround(targetObj.position, targetObj.up, mouseXRecord * rotateSpeed * Time.deltaTime);
    }

    void Translate()
    {
        Vector3 a = transform.position + (targetObj.position - transform.position) * mouseScrollRecord * translateSpeed * Time.deltaTime;
        float distance = Vector3.Distance(targetObj.position, a);

        if ((distance > minDistance) && (distance < maxDistance))
            transform.position = a;
    }

    IEnumerator StopRotate(float time)
    {
        yield return new WaitForSeconds(time);
        CancelInvoke("Rotate");
        mouseXRecord = 0;
    }

    IEnumerator StopTranslate(float time)
    {
        yield return new WaitForSeconds(time);
        CancelInvoke("Translate");
        mouseScrollRecord = 0;
        isInertiaTra = true;
    }
}


[Unity实战]相机观察物体

标签:相机

原文地址:http://blog.csdn.net/lyh916/article/details/44873443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!