Unity simple mobile camera

Unity3D makes a moving block (without moving the camera)_SMG_DSG's Blog-CSDN Blog

Following the code of the last article, we continue to write. In fact, simple movement is also very simple. We only need to use a function that keeps the camera facing the square.

Okay, without further ado, let’s get to the point.

First create a C# code file, name it "camera", double-click to open it

Now write inside

public Transform target;

Here we create a public variable of Transform type named target, which is used to receive the real-time coordinate data of the block to be faced.

and then write

transform.LookAt(target);

Here we use a function called LookAt in the transform class to make the entity face the target. The target inside is the target.

Okay, let’s take a look at the overall code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class carama : MonoBehaviour
{
    public Transform target;

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(target);    
    }
}

keep!

Go back to the Unity editor and drag camera.cs to the camera we are using to bind the entity to be controlled when the program is running.

At this point we will see that there is a target under Carama (script), which is the target public variable we just created, which is the entity we want to obtain in real time, but it is now in an empty state, we give it Join the Cube entity we created

The operation is very simple, just drag the Cube into it.

Okay, let’s run it. Let’s press ↑↓←→ to see if the square changes from small to large and from large to small. 

OK, this lesson is complete. I hope you can point out what I said incorrectly in this article. Thank you very much! ! !

Guess you like

Origin blog.csdn.net/SMG_DSG/article/details/131346993