Restore Scene window in Unity game Window Camera Action Enhanced Edition

Source: Internet
Author: User

The version that I wrote earlier looks really bad. Recently studied the official first-person script, the smooth transition of others really do not have to say. Borrowed a bit, wrote out a new more perfect control.

Our operation was preceded by the mouse input of the start coordinate and rotation coordinates. In fact, the official has a function ~

1 float Yrot = Input.getaxis ("Mouse X"); 2 float Xrot = Input.getaxis ("Mouse Y");

This can be obtained by the x-axis operation and the y-axis operation of the mouse respectively.

So why get the x-axis with Yrot, Xrot get the y-axis?

The left is the top view of the mouse, and the right is the three-dimensional coordinates in unity. As you can see, the translation of the x-axis of the mouse corresponds to the rotation of the y-axis in unity. The y axis is the same.

However, it is still not possible to copy the official wording, because the official writing is aimed at its own coordinates, that is, local. (Note: Localposition is not equal to the local coordinates of the object)

The camera of the scene window is the rotation for world.

You need to convert it here.

First we get the camera's current rotation angle, we initialize it at start

1     void Start () 2     {3         camerar = Camera.main.transform.rotation.eulerAngles; 4     }

Modify the rotation in the update with the form of Vector3

1             //Official Script2             floatYrot = Input.getaxis ("Mouse X");3             floatXrot = Input.getaxis ("Mouse Y");4 5Vector3 R = Camerar +NewVector3 (-xrot, Yrot, 0f);//plus the rotational distance6 7Camerar = Vector3.slerp (Camerar, R, 100f * time.deltatime);//Smooth Transitions8 9Transform.rotation = Quaternion.euler (Camerar);

Give the full script

1 usingUnityengine;2 usingSystem.Collections;3 4  Public classCameracotrel:monobehaviour {5 6     Private floatSpeed =100f;7     PrivateVector3 Camerar;8 9     voidStart ()Ten     { OneCamerar =Camera.main.transform.rotation.eulerAngles; A     } -      -     voidUpdate () the     { -Vector3 face = transform.rotation *Vector3.forward; -face =face.normalized; -  +Vector3 left = transform.rotation *Vector3.left; -left =left.normalized; +  AVector3 right = Transform.rotation *Vector3.right; atright =right.normalized; -  -         if(Input.getmousebutton (1)) -         { -             //Official Script -             floatYrot = Input.getaxis ("Mouse X"); in             floatXrot = Input.getaxis ("Mouse Y"); -  toVector3 R = Camerar +NewVector3 (-Xrot, Yrot, 0f); +  -Camerar = Vector3.slerp (Camerar, R, Speed *time.deltatime); the  *Transform.rotation =Quaternion.euler (camerar); $         }Panax Notoginseng  -         if(Input.getkey ("W")) the         { +Transform.position + + face * speed *Time.deltatime; A         } the  +         if(Input.getkey ("a")) -         { $Transform.position + = left * Speed *Time.deltatime; $         } -  -         if(Input.getkey ("D")) the         { -Transform.position + = right * speed *Time.deltatime;Wuyi         } the  -         if(Input.getkey ("s")) Wu         { -Transform.position-= face * speed *Time.deltatime; About         } $          -         if(Input.getkey ("Q")) -         { -Transform.position-= vector3.up * Speed *Time.deltatime; A         } +  the         if(Input.getkey ("e")) -         { $Transform.position + = Vector3.up * Speed *Time.deltatime; the         } the  the     } the}

Restore Scene window in Unity game window Camera Action hardening version

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.