Unity3D: Game decomposition-role movement and camera follow, unity3d follow

Source: Internet
Author: User

Unity3D: Game decomposition-role movement and camera follow, unity3d follow
In the game, we often have such operations: click a location in the scenario, the role is automatically moved to that location, and the role is always moving towards that location, the camera also keeps following the role. In some games, the mouse slides the screen and the camera rotates around the role. It seems like a simple operation, so how is it implemented? The above operations are divided into the following steps:Role Movement1. Move to the next path, linear interpolation, and curve interpolation. 2. Orientation of the role, always facing the next path.Camera follow role1. the camera's viewing angle determines the camera's height. the camera follows the distance, forward distance or straight line distance (that is, the horizontal side of the triangle or the oblique side length) 3. the camera always looks at the back of the role (the Y axis rotation angle is the same as the role) 4. technical Points of camera rotation around role: 1. vector 2. please forgive me for not registering the screen recording orzRole MovementThis includes the shift and direction, which means that the role is always moving in the direction of movement at the same time. In the picture on the left, the role moves from A to B, but the orientation is always forward, obviously not in line with the running display logic. The correct result is that the role is moving in the direction shown in the right figure. So what can we do to achieve this effect? The displacement is very simple. Coordinate interpolation from A to B. The second is rotating the role. Unity provides a methodQuaternion. LookRotation. The official explanation for this method is as follows: Quaternion. LookRotation

Static function LookRotation (forward: Vector3, upwards: Vector3 = Vector3.up): QuaternionDescription description Creates a rotation that looks along forward with the head upwards along upwards Creates a rotation along the forward (Z axis) and the head is watched along the constraints of upwards (Y axis. That is, create a rotation to make the Z axis orientation to the view Y axis up. Logs an error if the forward ction is zero. if the forward direction is 0, an error is recorded.

 

It is difficult to understand the description. There are many explanations for this method on the Internet, but there are different opinions. There is no simple and clear statement, which is more likely to mislead people.

We know the vector, including the size and direction. The size is easy to get, so how can we get the direction? In general, the vector can be divided into three components x, y, and z, and the angle of each component can be obtained through the trigonometric function in turn. Unity provides a simpler method, that is Quaternion. LookRotation,This method is used to obtain the direction of the input vector, that is, the rotation value, which is a Quaternary. The code is actually very simple, just a few lines. The main reason is to understand why
1 // calculate the vector from the current position to the next coordinate point 2 var vector = (posB-posA ). normalized; 3 // returns the vector direction 4 var rotation = Quaternion. lookRotation (vector ). eulerAngles; 5 // rotate the object to the next coordinate point in the direction of 6 transform. rotation = Quaternion. euler (0, rotation. y, 0); 7 // sets the object's coordinate 8 transform. position = posB;
Think about why Quaternion. Euler (0, rotation. y, 0) where the x and z directions are set to 0? Because the orientation of the role is determined by the deflection angle Yaw, that is, the Y axis, x and Z axes do not have deflection. If the rotation value of the x axis is changed, you will find that the role has the effect of pitching and rolling. Camera follow roleOkay, the orientation of the role is fixed. So how can I achieve this if I want my camera to keep following the role and the camera to see its back? That is, when the role rotates, the camera will rotate along with a fixed distance? We first calculate the camera position, and then rotate the camera toward the role's back. 1. Calculate the rotation value of the camera. here we need to specify the Pitch angle Pitch value of the camera, which is assumed to be 30 degrees. It can be adjusted according to the actual situation.
// The Pitch angle and yaw angle of the camera. The Y-direction yaw is consistent with the target object. Quaternion ro = Quaternion. Euler (Pitch, transform. rotation. eulerAngles. y, 0 );
2. Calculate the vector of the specified length Distance, which is parallel to the z direction of the world coordinate.
var vector = Vector3.forward * Distance;
3. Use the above camera rotation value to take the left by the vector obtained in step 2 and change the direction of the vector (the left multiplication vector of the Quaternary element changes the direction of the vector)
vector  = ro * vector;
4. Subtract the vector from the target position to obtain the coordinate point pointing to the target position, that is, the final position of the camera. (Why do we get the location? Let's go back and look at the vector knowledge)
var pos = transform.position - vector; 
5. Finally, assign the rotation value and coordinates to the camera, and then the camera completes the following effect. Is it very easy?
CameraGo.transform.position = pos;CameraGo.transform.rotation = ro;
1 // The pitch angle and yaw angle of the camera. The Y-direction yaw is consistent with the target object. 2. Quaternion ro = Quaternion. euler (Pitch, transform. rotation. eulerAngles. y, 0); 3 // rotate the vector 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 camera rotation around the role, we only need to change the Quaternion. euler (Pitch, transform. rotation. eulerAngles. in y, 0), transform. rotation. eulerAngles. the value y originally specifies the orientation of the camera towards the role. We can change this value to achieve the effect of the camera around the role. We can do this.
// Delta is the rotation angle around the role ~ 360.
Quaternion ro = Quaternion.Euler(Pitch, transform.rotation.eulerAngles.y + delta, 0)
Finally, the Appeal code is as follows, and the code is incomplete. Please complete it yourself:
1 // move the role 2 void SmoothMove () 3 {4 Vector3 [] vector3s = _ transDataList; // CurvePath. pathControlPointGenerator (_ transDataList); 5 int sample = _ transDataList. length * SampleRate; 6 7 _ movePtg + = Time. deltaTime * MoveSpeed; 8 9 // curve interpolation 10 transform. position = CurvePath. interp (vector3s, _ movePtg/sample); 11 12 // calculate the vector 13 var vector = (transform. position-_ prevPos ). normalized; 14 // obtain the vector direction 15 v Ar rotation = Quaternion. lookRotation (vector, Vector3.right ). eulerAngles; 16 // the influence of the position x and the z direction, which only applies to the deflection of the y Direction 17 rotation. x = 0; 18 rotation. z = 0; 19 20 // rotate the object to the direction of the next coordinate point 21 transform. rotation = Quaternion. euler (rotation); 22 23 24 _ prevPos = transform. position; 25 if (_ movePtg> = sample) 26 {27 ResetLocalData (); 28} 29} 30 31 // The camera follows 32 void FollowCamera () 33 {34 if (CameraGo = null) return; 35 36 if (UseFollow! = 0) 37 {38 // the camera's pitch angle and yaw angle. The Y-direction yaw is consistent with the target object. 39 Quaternion ro = Quaternion. euler (Pitch, transform. rotation. eulerAngles. y + Slider, 0); 40 41 // rotate the vector to 42 var distanceVector = ro * Vector3.forward * Distance; 43 var pos = transform. position-distanceVector; 44 CameraGo. transform. position = pos; 45 CameraGo. transform. rotation = ro; 46 return; 47} 48}

 

 

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.