今天看啥
热点:

[Unity实战]相机观察物体,unity观察物体


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


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


www.bkjia.comtruehttp://www.bkjia.com/Androidjc/979167.htmlTechArticle[Unity实战]相机观察物体,unity观察物体 在原有的旋转、缩放的基础上加上了“惯性”。 using UnityEngine;using System.Collections;public class CameraOb...

相关文章

相关搜索: 相机 unity3d

帮客评论

视觉看点