The camera tracking feature of Unity 2D game development Tutorial

Source: Internet
Author: User

The camera tracking feature of Unity 2D game development Tutorial

In the previous chapter, we created a simple 2D game. The sprite in this game has 3 states: idle, left, and right. This looks really cool! But the only 3 states limit the ability of the genie and the imaginary space of the game logic. It seems necessary to let the Elves have more state, and this is the main content of this chapter to explain.

Camera Tracking function

The game's elves can move around in the game scene, which is fine, but this leads to the problem that sprites may move beyond our horizons, or beyond the game view. In order to solve this problem, many games use "camera tracking" method, so that the position of the camera will move with the wizard. For example, in Super Marie, the Sprite is always in the center of the view, as shown in 2-1.

Figure 2-1 "Super Marie", the genie is always in the center of the game view

To add "camera tracking" to the game we develop, we need to use scripting to write such a logic. In the Scripts folder in Project view, create a new C # script, named Cameracontroller, to add the following code to this script:

  • Using Unityengine;
  • Using System.Collections;
  • 03
  • public class Cameracontroller:monobehaviour
  • 05 {
  • 06//Public attribute
  • 07//Indicates the current animation state of the Sprite
  • Public playerstatecontroller.playerstates currentplayerstate = PlayerStateController.playerStates.idle;
  • Gameobject playerobject = null; Represents a Sprite Object
  • Ten public float cameratrackingspeed = 1f; Indicates the camera's tracking speed
  • 11//Private Property
  • The private Vector3 lasttargetposition = Vector3.zero; Previous target Location
  • Private Vector3 currtargetposition = Vector3.zero; Next target Location
  • The private float currlerpdistance = 0.0f;
  • 15//Method
  • + void Start ()
  • 17 {
  • Vector3 playerpos = playerObject.transform.position; Record the location of the sprite
  • Vector3 camerapos = transform.position; Record the location of the camera
  • Vector3 starttargpos = Playerpos;
  • Lasttargetposition = Starttargpos;
  • Currtargetposition = Starttargpos;
  • 23}
  • 24//Join Subscribers list
  • void Onenable ()
  • 26 {
  • Playerstatecontroller.onstatechange + = Onplayerstatechange;
  • 28}
  • 29//exit from the Subscriber list
  • void Ondisable ()
  • 31 {
  • Playerstatecontroller.onstatechange-= Onplayerstatechange;
  • 33}
  • 34//real-time recording game Sprite current animation status
  • void Onplayerstatechange (Playerstatecontroller.playerstates newstate)
  • 36 {
  • Panax Notoginseng currentplayerstate = newstate;
  • 38}
  • lateupdate void ()
  • 40 {
  • 41//According to the current sprite animation status, real-time update
  • Onstatecycle ();
  • 43//Move the camera to the target location
  • Currlerpdistance + = Cameratrackingspeed;
  • Transform.position = Vector3.lerp (lasttargetposition, currtargetposition, currlerpdistance);
  • 46}
  • onstatecycle void ()
  • 48 {
  • On-Switch (currentplayerstate)
  • 50 {
  • Wuyi Case PlayerStateController.playerStates.idle:
  • Trackplayer ();
  • a break;
  • PlayerStateController.playerStates.left Case:
  • Trackplayer ();
  • a break;
  • PlayerStateController.playerStates.right Case:
  • Trackplayer ();
  • a break;
  • 60}
  • 61}
  • Trackplayer void ()
  • 63 {
  • 64//Get and save the coordinates of the camera and sprite in the world coordinate system
  • Vector3 currcampos = transform.position;
  • Vector3 currplayerpos = playerObject.transform.position;
  • Lasttargetposition = Currcampos;
  • Currtargetposition = Currplayerpos;
  • Currtargetposition.z = currcampos.z; Ensure that the values on the z axis of the camera are not changed
  • 70}
  • 71}

Assign this script to the main camera object in the hierarchy view, select the latter, and then set the following properties of this script component in the Inspector view, as shown in 2-2.

    • Q Player object:player. Represents the sprite object to be traced by the camera;
    • Q Camera Tracking speed:1. This property value range is 0~1, if it is 0 o'clock, the camera will not track the genie, if 1 o'clock, the camera can be in an instant tracking to the Sprite object;

Figure 2-2 Settings for each property on the script component of the Main camera object

Here are a few things to note about this script:

    • Q Script 69 lines of code so that the camera and the sprite object does not overlap. To allow the camera to track the game Genie in real time, just update the camera's location in real time. is to make its position consistent with the position of the sprite, but they cannot have the same value in the z direction, as shown in 2-3. Otherwise, the camera and the genie will coincide, so that the game view of the Sprite object disappears, as shown in 2-4.

Figure 2-3 the camera and the sprite have different values in the z direction

Figure 2-4 the camera has the same value as the sprite in the z direction

    • Q Script 45 line Vector3.lerp () function is the main function to complete the real-time camera tracking function. This function moves an object from one position to another at a certain speed;

Run the game, and then use the arrow keys on the keyboard to control the sprite move around, you will find the game view will be moved with the elves, even if the elves because of the fall off the ground, is no exception, 2-5 is shown.

Figure 2-5 Camera tracking, sprites are always in the center of the game view

This article is selected from: Unity 2D Game Development QuickStart University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!

The camera tracking feature of the Unity 2D game development Tutorial

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.