Using unity to achieve camera control in Minecraft

Source: Internet
Author: User

In the process of learning Unity , the most basic and most important part is the lens control .

Lens control is through the mouse, keyboard and other input device operation, to achieve in-game navigation. The essence of Unity is to transform the position (position) and rotation (rotation)of the camera object by receiving the mouse's movement and keyboard keys, These two values are located in the camera's transform component (conponent) .

Here we learn the first-person lens control by implementing the mirror- head control of the creative mode in my world.

In my world, the orientation (rotation) of the lens is controlled by the translation of the mouse in the X and Y directions, and the left and right movement of the lens is the usual WASD key, arising ( Move up ) to the SPACEBAR, and land ( Move Down ) to the Shift key.

Let me give you the complete code and the Demo setup step. Once the operation is successful, we will parse the code again.

Create Demo

1. Create a new Unity project.

2. Save the following code as BasicCameraControl.cs, and put it under the Assets folder of the Unity project .

usingUnityengine;usingSystem.Collections; Public classbasiccameracontrol:monobehaviour{floatYaw =0;//Yaw angle, angle of rotation around y-axis    floatPitch =0;//pitch angle, angle of rotation around the z-axis    Const floatRoll =0;//roll angle, angle of rotation around x-axis    voidfixedupdate () {//Move forward or backward        if(Input.getkey (KEYCODE.W)) Transform.position+ = Vector3.clampmagnitude (Transform.forward,0.1f); Else if(Input.getkey (KEYCODE.S)) Transform.position-= Vector3.clampmagnitude (Transform.forward,0.1f); //Move left or right        if(Input.getkey (KEYCODE.A)) Transform.position-= Vector3.clampmagnitude (Transform.right,0.1f); Else if(Input.getkey (KEYCODE.D)) Transform.position+ = Vector3.clampmagnitude (Transform.right,0.1f); //Move up or down        if(Input.getkey (keycode.space)) Transform.position+=NewVector3 (0,0.1f,0); Else if(Input.getkey (keycode.leftshift)) Transform.position-=NewVector3 (0,0.1f,0); //RotatePitch-= Input.getaxis ("Mouse Y") *5; Pitch= Mathf.clamp (Pitch,- -, -); Yaw+ = Input.getaxis ("Mouse X") *5; Transform.rotation=Quaternion.euler (pitch, yaw, roll); }}

3. Add this script component to the default camera object (Main camera) in the scene. To do this, select Main Camera, Click the Add Component button at the bottom of the Inspector, click the Scripts menu, click Basic Camera Control is complete.

4. add some objects in the center of the scene as a reference when moving the camera, such as a cube.

5. Click the Play button, move the mouse to turn, and press WASD, SPACEBAR, and Shift to move.

Create Demo

1. Lens panning Control

The panning of the lens is actually the change of the x, Y, z component of the Camera.transform.positon after the keyboard is detected with WASD, space, and Shift pressed.

What we need to note here is that they component, which is rising and falling, is always parallel to the Y axis of the World coordinate system , so modify the y component directly :

New Vector3 (00.1f0);

But for forward and backward and left and right panning is not the same, because our movement in these four directions is also related to the direction we are facing. So we get towards the Tansform.forward and transform.right and then we add and subtract with the transform.position .

Use the Vector3.clampmagnitude () function to slow down the speed of movement to the appropriate level.

2. Lens Rotation Control

About the rotation of the lens we might be able to do this with one line of code:

Transform. Rotate (-Input.getaxis ("Mouse Y"5, Input.getaxis ("Mouse X " 5 0);

But there is no guarantee that our lenses will always remain perpendicular to the ground. By constantly drawing circles on the screen, we can achieve handstand. We're not a circus, so this is definitely not what we want.

To prevent this from happening, we offset the components of the rotation angle first, and then convert the quaternion.euler() function to a four-dollar number.

3. Why use fixedupdate ()

the difference between update () and fixedupdate () is that update () occurs with the render process. and fixedupdate () is in accordance with a fixed period of time, that is, even because of the number of characters on the screen, the refresh rate is reduced, the function can be guaranteed to perform a constant number of times.

Knowing this, we can imagine. If we run a race in Minecraft , a high-quality desktop computer runs at 60FPS , and a laptop runs on average 30FPS . If we use Update () to drive the character movement, then the characters on the notebook definitely move without the desktop fast. This certainly does not conform to the principle of fairness, so we must use fixedupdate () here.

"End"

Using unity to achieve camera control in Minecraft

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.