[Unity Combat] camera watch objects

Source: Internet
Author: User

Added "inertia" on the basis of the original rotation and scaling.


Using unityengine;using System.collections;public class Cameraobserve:monobehaviour {public Transform targetobj;    public float rotatespeed = 100f;    public float translatespeed = 10f; public float mindistance = 3f;//Zoom Observe the minimum distance public float maxdistance = 15f;//Zoom Observe the maximum distance public bool Isinertiarot = Fals    e;//whether the left button will continue to rotate a few degrees after the public bool Isinertiatra = false;//does not roll after the wheel will continue to move a small amount of distance 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)) {in            Vokerepeating ("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) * Mousescroll        Record * 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 Combat] camera watch objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.