Unity3d: Game decomposition of character movement and camera follow

Source: Internet
Author: User

In the game, we often have this action, click a location in the scene, the character automatically moved to that position, while the character has been moving towards that position, and the camera will always follow the character movement. Some games, the mouse slide screen, the camera will revolve around the character. Seemingly very simple operation, then how exactly is the implementation of it? Let's break down these operations into the following steps the movement of a character1. Move to the next route point, linear interpolation, curve interpolation 2. Character facing, always facing the next road. Camera Follow character1. The camera overlooks the angle and determines the height of the camera 2. Camera follow distance, forward distance or straight line distance (that is, the triangular horizontal side length or hypotenuse length) 3. The camera always looks at the back of the character (the y-axis rotation angle is consistent with the character) 4. Camera rotation around the role of technical points: 1. Vector 2. Rotation first look at the effect, please forgive me not registered screen video Orz role MoveThis includes displacement and direction, which is where the character moves in the same direction as the moving character. The diagram on the left, where the character moves from A to B, is facing forward and is clearly inconsistent with the running display logic. The correct performance is shown on the right side of the image, the character faces moving direction. So what do we have to do to achieve this effect? The displacements are simple, and the coordinates of A to B are interpolated. followed by a rotating character, unity provides a way quaternion.lookrotation。 The official explanation for this method is as follows: Quaternion.lookrotation gaze rotation
static function lookrotation (Forward:vector3, Upwards:vector3 = vector3.up): quaterniondescription description CRE Ates a rotation that looks along forward with the the the head upwards along upwards create a rotation along the forward (z-axis) and head along the upwards (y-axis) of the constraint gaze 。 That is, create a rotation so that the z  -axis faces the viewif  is zero. If the forward direction is 0, an error is logged. 

It is not difficult to understand the description of light. There are a lot of explanations for this method on the Internet, but there are a few simple and clear statements that are more misleading.

We know the vectors, which contain the size and direction. Size is easy to get, so how do you get directions? Conventionally, the vector can be divided into X, Y, z three components, and then the trigonometric functions in order to obtain the angle of a component. Unity provides an easier way to Quaternion.lookrotation,This method is to get the direction of the incoming vector, that is, the rotation value, is a four-dollar number. The code is actually very simple, just a few lines. The main reason is to understand why
1             //A vector that calculates the current position to the next coordinate point2             varVector = (PosB-PosA). normalized;3             //get the direction of the vector4             varrotation =quaternion.lookrotation (vector). Eulerangles;5             //rotates the object to the direction of the next coordinate point6Transform.rotation = Quaternion.euler (0, ROTATION.Y,0);7             //set the coordinates of an object8Transform.position = PosB;
Think about why Quaternion.euler (0, ROTATION.Y, 0) Here the X and z directions are all filled 0? Because the direction of the character is based on the deflection angle yaw, which is determined by the Y axis, the x and Z axes are not deflected, if you change the z axis rotation value of the x axis, you will find that the character will have pitch, tumbling effect. Camera Follow characterWell, the direction of the character has been solved. So, what if I want the camera to go along with the character, and the camera always sees the back of the character, that is, when the character spins, the camera rotates and stays at a fixed distance? We first calculate the position of the camera and then turn the camera toward the back of the character. 1. Calculate the camera rotation value, here you need to specify the camera's pitch angle pitch value, assumed to be 30 degrees, can be adjusted according to the specific situation
// camera pitch and yaw angle, Y-direction yaw and target object consistent 0);
2. Calculate the vector of the specified length distance, which is parallel to the world coordinate Z direction
var vector = Vector3.forward * Distance;
3. Use the above camera rotation value to the left by the second step to get the vector, change the direction of the vector (four number of left multiplication vector, change the direction of the vector)
Vector  = ro * vector;
4. Use the target position minus the vector to get the coordinate point pointing to the target position, which is the final position of the camera. (Why do you get the position, go back to the knowledge of the vector?)
var pos = transform.position-vector;  
5. Finally, the rotation value and coordinates are assigned to the camera, the camera completes the follow effect, is not very simple
CameraGo.transform.position == ro;
 1  //  The camera's pitch and yaw angles, Y-direction yaw and target object are consistent  2  quaternion ro = Quaternion.euler (Pitch, Transform.rotation.eulerangles.y, 0   3  //  Give vector rotation  4  var  distancevector = ro * Vector3.forward * Distance;  5  var  pos = transform.position- Distancevector;  6  CameraGo.transform.position = POS;  7  CameraGo.transform.rotation = ro; 
As for the camera rotation around the character, we just need to change the Quaternion.euler (Pitch, transform.rotation.eulerangles.y, 0) The value in Transform.rotation.eulerangles.y this value is the direction of the specified camera toward the character, we change this value, we can achieve the effect of the camera around the role. We can do this.
// The delta is the rotation angle 0~360 around the character rotation.
0)
Finally, the appeal code is as follows, the code is not complete, please fill in your own:
1        //role Move2         voidSmoothmove ()3         {4vector3[] Vector3s = _transdatalist;//Curvepath.pathcontrolpointgenerator (_transdatalist);5             intSample = _transdatalist.length *samplerate;6  7_MOVEPTG + = Time.deltatime *Movespeed;8  9             //curve interpolationTenTransform.position = Curvepath.interp (vector3s, _MOVEPTG/sample); One   A             //A vector that calculates the current position to the next coordinate point -             varVector = (Transform.position-_prevpos). normalized; -             //get the direction of the vector the             varrotation =quaternion.lookrotation (vector, vector3.right). Eulerangles; -             //the effect of the place X and z direction, only the Y-direction deflection -Rotation.x =0; -Rotation.z =0; +   -             //rotates the object to the direction of the next coordinate point +Transform.rotation =Quaternion.euler (rotation); A   at   -_prevpos =transform.position; -             if(_MOVEPTG >=sample) -             { - Resetlocaldata (); -             } in         } -   to         //Camera Follow +         voidFollowcamera () -         { the             if(Camerago = =NULL)return; *   $             if(Usefollow! =0)Panax Notoginseng             { -                 //camera pitch and yaw angle, Y-direction yaw and target object consistent thequaternion ro = Quaternion.euler (Pitch, Transform.rotation.eulerangles.y + Slider,0); +   A                 //give a rotation to a vector the                 varDistancevector = ro * Vector3.forward *Distance; +                 varpos = transform.position-Distancevector; -CameraGo.transform.position =Pos; $CameraGo.transform.rotation =ro; $                 return; -             } -}

Unity3d: Game decomposition of character movement and camera follow

Related Article

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.