Restore Scene window camera operation in Unity game window

Source: Internet
Author: User

Recently in the AI, debugging the program is always to adjust the camera's perspective. Gray often uncomfortable and then wrote a script. Compare the camera operation in the scene window so it mimics the same action script.

Prime Minister, we need to know how the camera works under scene.

1.WASD respectively control the displacement of the left and right, note: Displacement is the current angle of view as the benchmark.

2.QE respectively control the upper and lower, note: This is for world coordinates.

3. Right mouse button controls free view rotation. Difficulties

For the first article, the main problem is now the player facing the problem. How do I know where I face.

        Vector3 face = transform.rotation * vector3.forward;         = face.normalized;

This code is the player's direction, the current player's rotation angle on the front of the vector, is the player's face toward the direction, because we want direction, so the unit to quantify.

Here's how to control the first one.

Vector3 face = transform.rotation *Vector3.forward; face=face.normalized; Vector3 Left= Transform.rotation *Vector3.left; Left=left.normalized; Vector3 Right= Transform.rotation *Vector3.right; Right=right.normalized; if(Input.getkey ("W") ) {transform.position+ = face * Speed *Time.deltatime; }        if(Input.getkey ("a") ) {transform.position+ = left * Speed *Time.deltatime; }        if(Input.getkey ("D") ) {transform.position+ = right * speed *Time.deltatime; }        if(Input.getkey ("s") ) {transform.position-= face * speed *Time.deltatime; }

The second one does not have to say more. On the Code

        if (Input.getkey ("q"))        {            -= vector3.up * Speed * time.deltatime        ;        } if (Input.getkey ("e"))        {            + = vector3.up * Speed * time.deltatime;        }

The hardest of the third article. Thought for a while there are two solutions, but all of them are flawed

1) convert uniformly into vectors to calculate

2) unified conversion into quaternion to calculate

Advantages and Disadvantages

1) When the rotation is not smooth, but in place fast

2) When rotating more than the first smooth some also some Hucaton, but in place unpleasant even not in place.

For both of these, the main idea is to take the mouse slide unit vector and multiply the speed.

Both of these designs are designed to be a problem for the pit daddy.

When the mouse slides up and down:

For the mouse, y-axis plus minus

For rotation, the x-axis minus

When the mouse slides left and right:

For the mouse, the x-axis minus plus

For rotation, the y-axis minus plus

So this piece of code is important.

            // _rot is the object's current rotation value, Movepos is the modified value, and finally the value rotated            2 // * * can be adjusted speed,            the bigger the faster 2 ;             2;    

Then for the first way all become vector processing we will use transform.eulerangles;

Code for

            Vector3 Save = input.mouseposition;             = Save- mousedownpos;             = movepos.normalized;             = transform.rotation.eulerAngles;             2 ;             2 ;             2 ;             = _rot;            Debug.Log (Movepos);             = Save;

For the second way all becomes quaternion processing we use QUATERNION.SLERP

Code for

 Vector3 Save = input.mouseposition;            Vector3 movepos  = Save- Mousedownpos;            Movepos  = movepos.normalized;            Vector3 _rot  = Transform.rotation.eulerAngles;            _rot.x -= movepos.y * 2  ;            _rot.y  + = movepos.x * 2  ;            _rot.z  + = movepos.z * 2  ;            quaternion moverot  = Quaternion.euler (_rot); Transform.rotation  = Quaternion.slerp (transform.rotation, Moverot, Time.deltatime * 30             ); Mousedownpos  = Save; 

Finally give the full update code

    Private floatSpeed =5; PrivateVector3 Mousedownpos; voidUpdate () {Vector3 face= Transform.rotation *Vector3.forward; face=face.normalized; Vector3 Left= Transform.rotation *Vector3.left; Left=left.normalized; Vector3 Right= Transform.rotation *Vector3.right; Right=right.normalized; //Debug.Log (transform.rotation * vector3.forward + "," + transform.rotation * vector3.left + "," + transform.rotation * Vector3.right);        if(Input.getmousebuttondown (1) ) {Mousedownpos=input.mouseposition; }        if(Input.getmousebutton (1))        {            //Vector ProcessingVector3 Save =input.mouseposition; Vector3 Movepos= Save-Mousedownpos; Movepos=movepos.normalized; Vector3 _rot=Transform.rotation.eulerAngles; _rot.x-= Movepos.y *2; _rot.y+ = Movepos.x *2; _rot.z+ = Movepos.z *2; Transform.eulerangles=_rot;            Debug.Log (Movepos); Mousedownpos=Save; //quaternion processing//Vector3 Save = input.mouseposition; //Vector3 movepos = Save-mousedownpos; //movepos = movepos.normalized; //Vector3 _rot = transform.rotation.eulerAngles; //_rot.x-= Movepos.y * 2; //_rot.y + = movepos.x * 2; //_rot.z + = movepos.z * 2; //quaternion Moverot = Quaternion.euler (_rot); //transform.rotation = Quaternion.slerp (transform.rotation, Moverot, Time.deltatime * 30); //mousedownpos = Save;        }        if(Input.getkey ("W") ) {transform.position+ = face * Speed *Time.deltatime; }        if(Input.getkey ("a") ) {transform.position+ = left * Speed *Time.deltatime; }        if(Input.getkey ("D") ) {transform.position+ = right * speed *Time.deltatime; }        if(Input.getkey ("s") ) {transform.position-= face * speed *Time.deltatime; }                if(Input.getkey ("Q") ) {transform.position-= vector3.up * Speed *Time.deltatime; }        if(Input.getkey ("e") ) {transform.position+ = Vector3.up * Speed *Time.deltatime; }    }

Restore Scene window camera operation in Unity game window

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.