[Introduction to Unity] The movement of objects
Hello everyone, I am Lampard~~
Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks
(1) Using scripts to drive objects to move
(1) Make a moving car
Looking back at the previous article, we can already use scripts to get the information of the mounted object, and in the update method, make the object move . In this blog, we make a car and use the script from the last lesson to see if the car can run
First, we recreate the car that was mounted with the Audio control before, and then reset its Position and Rotation values to 0. It can be found that the current direction of the car is exactly the direction of our Z-axis
Therefore, in the script of the last lesson, we modified the change of X in each frame to the change of Z, which is more in line with the impression of the car driving towards the front.
Then drag the script to the car object and run it to see:
(2)transform.Translate
The previous script was to create a Vector3 value and then reassign it to transform.localPosition, but in fact there is a Translate method that can help us complete this task, just pass in the position information that needs to be changed
The following is the definition of the Translate method:
transform.Translate is a method of the Transform component in the Unity engine, used to translate objects. This method accepts a three-dimensional vector as an argument, indicating the distance and direction to translate. The units of translation distance and direction are the units in the world coordinate system
So our code can be changed to this:
void Update()
{
Vector3 localPos = this.transform.localPosition;
float DisPreSec = 6f;
//localPos.z += DisPreSec * Time.deltaTime;
//this.transform.localPosition = localPos;
this.transform.Translate(0, 0, DisPreSec * Time.deltaTime);
}
(2) Change the direction of movement
(1) Moving towards the red flag
Assuming that this car has a moving target, we need to move towards the red flag object, how to proceed to the end point?
First create a red flag object, and then place it anywhere
(2) Get the red flag object
Then in order to make the front of the car face the red flag, we need to get the position of the red flag. Before getting the position of the red flag, we need to get the object of the red flag in the script
How to do it? Red flags can be found through the static method GameObject.Find
GameObject.Find is a method in the Unity engine, which is used to find the game object with the specified name in the scene. This method accepts a string as a parameter, indicating the name of the game object to be found , and returns a GameObject object representing the game object
The usage is also very simple, just pass in the name of the target directly.
GameObject flag = GameObject.Find("红旗");
(3)transform.LookAt
After obtaining the object of the red flag, we can adjust the direction of the car in the next step. Of course, we can manually calculate the current cheap angle of two objects, but Teacher Afa directly provides us with a better interface: transform.LookAt
transform.LookAt is a method in the Transform component of the Unity engine, which is used to make the object face the specified target position. This method accepts a three-dimensional vector as a parameter, indicating the target position that needs to be oriented
You only need to call this method to turn the head of the car towards the red flag
void Update()
{
GameObject flag = GameObject.Find("红旗");
this.transform.LookAt(flag.transform);
Vector3 localPos = this.transform.localPosition;
float DisPreSec = 6f;
this.transform.Translate(0, 0, DisPreSec * Time.deltaTime);
}
Take a look at the effect:
(4) Code optimization
First of all, every second is updated 60 times, we don’t need to let the front of the car lookAt the red flag every time, we only need to execute it once, so we can define the red flag object as a script global variable, so that we only need to lookAt once in the start method on the line
Then, after the car drove towards the red flag, it ran over directly, which seemed unscientific. We can make the car stop in front of the red flag . The operation is very simple. We only need to judge that the current distance is close enough. If so, we don’t need to move. Add an if judgment
void Update()
{
float distance = Vector3.Distance(this.transform.position, flag.transform.position);
if (distance > 3f) {
Vector3 localPos = this.transform.localPosition;
float DisPreSec = 6f;
this.transform.Translate(0, 0, DisPreSec * Time.deltaTime);
}
}
Take a look at the effect:
Well that's all for today, thanks for reading! ! !
Like and follow! ! !