[Unity實戰]相機觀察物體,unity觀察物體

來源:互聯網
上載者:User

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.