Project training-----unity multiplayer game development--the third part

review

This time we mainly explain some problems and ideas encountered in the game development process.
This time, we mainly talk about the way characters move and how they drive.

main body

Content 1: Character movement control method

The way the characters move: as far as I know. There are three types of character movement.

One is to control the movement according to the keyboard, the common one is wsad and the keyboard control of up, down, left, and right, such as the glory of the king.

The second is to use the mouse to control, where the mouse clicks, where the character moves, and automatically finds the route. It can also be used for AI, such as common games such as League of Legends.

The third is to realize the movement of the character by controlling the balance. This is mainly used for the character control of mobile games, such as hungry sharks and some racing games, etc., which is biased towards somatosensory operation.
The following is based on these aspects.

Method 1: keyboard control

There are two main aspects to control character movement through the keyboard.
The first is the need to read keyboard input:

		horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

The second is to transfer the read data to the character to achieve movement. There are two ways to move, one is to change the transform, and the other is to change the position of the rigid body to realize the movement of the character.

The keyboard controls the character's transform movement

For transform, each object has a transform, which is used to indicate the character.
insert image description here

Here, we can see that the transform component has three properties, namely position, rotation and scale.
represent position, rotation and scale, respectively. Character movement is mainly controlled by position and steering.

    private Transform player;
	void Start()
    {
        player = this.transform;
    }

	 void Update()
    {
       horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        if( horizontal !=0 || vertical != 0 )
        {
            player.Translate(new Vector3(horizontal,0,vertical) * Time.deltaTime * moveSpeed); //这个是时间,每帧执行
        }
    }
     

features

Advantages: Simple and convenient
Disadvantages: It may cause the situation of passing through the wall, and it may cause shaking when colliding with objects.

The keyboard controls the rigidbody movement of the character

For using a rigid body, first get the rigid body

 void Start()
    {
        rigidbody = GetObjectComponent<Rigidbody>(gameObject);//获取自身的Rigidbody组件
    }

Then move by changing the speed of the rigid body

rigidbody.velocity = new Vector3(horizontal,0,vertical) * moveSpeed;//3d,主要改变x和z轴,乘以一个移动速度,这个速度可以自己设置
rb.velocity = new Vector2(xVelocity , xVelocity)* speed; //2d,这个是x和y,没有z。

Features:

Advantages: You can better control the position of the character, easy to move, and will not shake.
Disadvantages: Compared with transform, the comparison is more complicated. In comparison, the priority is to control the rigid body to achieve movement.

Summarize

Through this blog, it mainly describes how to use the keyboard to control the movement of characters. It is mainly controlled by rigid body and transfrom. Through these two control methods, comparative analysis was carried out respectively, and it was concluded that rigid body control is better, but direct transform is simpler.

Guess you like

Origin blog.csdn.net/qq_53259920/article/details/125113110
Recommended