"Komatsu teaches you to travel development" "Unity practical Skills" character head following lens rotation

Source: Internet
Author: User


This is a comparison of the scene on the end of the tour, when the character is displayed, when the camera rotates around the character, the character head follows the lens rotation. such as Tianya Moon Knife.





This is rare on the hand, but the implementation is no different.



First of all, in general, find the head node of the model, directly with the LookAt point to the camera, but the general demand is not so simple.



For example, beyond the head twist limit, the head needs to interpolate back to the original point, when the lens from the outside to the limit, need to interpolate back. At this time LookAt will not be able to use.



In more cases, the head itself coordinate system is not on the world axis, may be rotated more than 90 or the output of the prefab is crooked, and so on, these cases are not able to use LookAt.



So let's write it by myself.



Here are two options, one with four yuan rotation, one with Euler angle localeulerangles.



The two are like the internet saying, each has good or bad



The value of Euler's angle is the inspector value on unity, which is very intuitive.



But the biggest problem is the deadlock. And this is no way to solve.



If this is the way it is used, direct assignment


SceneCameraObject.transform.localEulerAngles = tar;


The second option is to use a four-dollar number to rotate.



The way it's used here is


mHead.rotation = Quaternion.AngleAxis(rot.x -180, -mHead.right) * mHead.rotation;//传递过来的数据多了180,所以需要剪掉  mHead.rotation = Quaternion.AngleAxis(rot.y, mHead.forward) * mHead.rotation;


Rotates the given angle in the upper and lower left and right two directions.
The benefit is also obvious, and the given angle is rotated by the axis.



* Here we see a 180-degree correlation, this one.



We chose the second, four-dollar rotation.



So the key is to have an offset angle



This mainly depends on your data source, since it is to do the head follow the lens rotation, generally because your finger drag causes the lens rotation,



So we get the coordinates of the data after the finger is dragged, minus X, y of the original coordinates. Gets the horizontal longitudinal angle offset value. Pass it over.



In normal terms, this can be rotated.



The next step is to interpolate the movement so that the head rotates more realistically.



Interpolation is nothing more than calling Vector3.smoothdamp or Mathf.smoothdamp. Define a few variables, here you can read my another article



"Komatsu teaches you to travel by hand to develop" unity practical skills "unity more beautiful displacement
http://blog.csdn.net/chrisfxs/article/details/50436738



But there is a very serious problem here. That's because 0 degrees is  degrees. If you reduce from 10 degrees to negative, will be converted to  degrees, is originally from 10 interpolation to 10. The result becomes from 10 interpolation to 350 ...
At this time can imagine, originally to turn left the process, suddenly head reverse rotation  degrees ...



So there's the number above-180.



Because I found that the threshold of 0 degrees is in front of me. And according to the demand, my right behind I do not need to rotate. So I just need to put the threshold in the back.



It's so witty, like me.



So in the passed value, each horizontal drag difference I added 180 degrees.


float degressX = angle2.x - angle1.x;  float degressY = angle2.y - angle1.y + 180;//加180是为了把临界点放在身子后面,好处理  Vector3 vec = new Vector3(degressX,degressY,0);  GameSystemBase.GameModelSys.RotateHead(vec);


So it's going to be back in the final spin.
Perfect implementation.



* In the middle can also have some negative questions, try to make him a positive to solve



Such as


float value = tar.y > 0 ? tar.y % 360 :tar.y% 360;  mCurrVec3.x =  mCurrVec3.x < 0 ? mCurrVec3.x  : mCurrVec3.x;


These are the methods.



Because it is a company project, it is not easy to display the details.
Welcome to discuss



"Komatsu teaches you to travel development" "Unity practical Skills" character head following lens rotation


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.