Project Training--Unity Multiplayer Game Development--Part Four

review

Last time we mainly explained some problems and ideas encountered in the game development process, mainly about how to use the keyboard to control the movement of characters. It is mainly controlled by rigid body and transfrom.

This time we mainly talk about another way of character movement.

main body

Content 1
This method is a method of querying and summarizing from the Internet, and it is also a kind of control through the keyboard. It is mainly used to control the movement of the character through the character controller. For character control, such as limiting the maximum slope of the character climbing, the height of the step, etc., this is equivalent to the component Nav, which is usually used for AI human-machine.

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

        //不考虑重力作用
        //characterController.Move(new Vector3(horizontal,0,vertical) * Time.deltaTime *moveSpeed);

The above mainly uses a controller to realize the movement of the characters. Specifically, you can experiment with the effect, because it is not involved in the project, so it is not in the test.

Content 2: Character movement control method – mouse control

This time we explain the mouse control, which is mainly used for the character movement control of the fourth game.

What about mouse control?

We mainly consider two aspects. First, how to determine the mouse click point. Second,

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		//主要通过一个射线来实现位置的确定,而人物的走动主要是通过nav自动寻路来完成的。需要设置人物的行走,可以行走的最大坡度以及可以迈出的最大高度。
        if (Physics.Raycast(ray,out hitInfo))
        {
           //切换鼠标贴图,根据不同的触碰类型点击出现不同的指针贴纸
            switch (hitInfo.collider.gameObject.tag)
            {
                case "Ground":
                    //设置移动的指针类型
                    Cursor.SetCursor(target, new Vector2(16, 16), CursorMode.Auto);
                    break;
                case "Enemy":
                    //设置移动的指针类型
                    Cursor.SetCursor(attack, new Vector2(16, 16), CursorMode.Auto);
                    break;
            }
        }
		transform.LookAt(target.transform);//转向移动目标


mouse control

	public event Action<Vector3> onMouseClicked;//获取一个event事件
    public event Action<GameObject> onEnemyClicked;//点击物体的事件 
//鼠标控制
    void MouseControll()
    {
        if(Input.GetMouseButtonDown(0) && hitInfo.collider != null)//按下了鼠标左键,并且射线有碰撞焦点
        {
            if (hitInfo.collider.gameObject.CompareTag("Ground"))
            {
                onMouseClicked?.Invoke(hitInfo.point);
            }
            if (hitInfo.collider.gameObject.CompareTag("Enemy"))
            {
                onEnemyClicked?.Invoke(hitInfo.collider.gameObject);
            }
        }
    }

Add method to mouse

        MouseManager.Instance.onMouseClicked += MoveToTarget;//添加订阅的方法
        MouseManager.Instance.onEnemyClicked += EventAttack;//添加攻击的方法


moveToTarget method

 public void MoveToTarget(Vector3 target)//参数保存一致
    {
        StopAllCoroutines();//打断移向目标
        agent.isStopped = false; //重新设置可以移动
        agent.destination = target;//将 位置定位到这里
    }

As for the method of attack, it is not involved here, so let's not talk about it.
insert image description here
The above is the nav controller of this character, which includes type, speed, angular velocity, acceleration, avoidance of obstacles, height and range of obstacles that can be crossed, etc.

Summarize

Through this sharing, I mainly talked about the remaining two methods of character control. Although they are not used as much as the previous two methods, they are also very common in games.

Guess you like

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