Unity3d implements keyboard and mouse to control character walking at the same time

control character walking

1. Add the CharacterController component to the character, and use the SimpleMove of the CharacterController to move the keyboard. Clicking the ground with the mouse to make the character walk mainly uses the way of automatic pathfinding. When combining, the else in the keyboard method is ignored to stop the animation of the character, so when the ground is clicked, the character always has no animation. Adding the Boolean value isMouse solves this small problem.

using System;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMovement : MonoBehaviour
{
    
    
    //鼠标控制行走
    private NavMeshAgent agent;
    Camera cam;
    private Animator animator;

    //键盘控制行走
    private CharacterController characterController;
    private float turnSpeed = 14f; // 转向速度
    private float speed = 4.4f;

    //控制变量
    private bool isMouse;

    void Start()
    {
    
    
        agent = gameObject.GetComponent<NavMeshAgent>();//初始化navMeshAgent
        cam = Camera.main;//取到相机
        animator = GetComponent<Animator>();
        // 获取角色控制器组件
        characterController = GetComponent<CharacterController>();
    }
    void Update()
    {
    
    
        MouseMove();
        KeyBoardControl();
    }


    private void MouseMove()
    {
    
    

        if (Input.GetMouseButtonDown(0))
        {
    
    
            isMouse = true;
            characterController.enabled = false;
            animator.SetBool("isMoving", true);
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);//从当前点击的位置发射一条射线
            if (Physics.Raycast(ray, out RaycastHit hit))//判断当前射线有没有射中物体
            {
    
    
                if (hit.transform.CompareTag("Ground"))//判断射中的物体是不是地面
                {
    
    
                    //让cube移动过去
                    agent.SetDestination(hit.point);//设置目标点
                    
                    print(hit.point);
                }
            }

        }
        if (!agent.pathPending && agent.remainingDistance < 0.5f)
        {
    
    
            isMouse = false;
            animator.SetBool("isMoving", false);
            characterController.enabled = true;
        }
    }
    private void KeyBoardControl()
    {
    
    
        if (isMouse)
        {
    
    
            return;
        }
        // 水平移动 返回的值是浮点数 区间[-1,1],按A是-1 按D是1,正好对应左右
        float h = Input.GetAxisRaw("Horizontal");
        // 垂直移动 返回的值是浮点数 区间[-1,1],按S是-1 按W是1,正好对应上下
        float v = Input.GetAxisRaw("Vertical");
        // 使用角色控制器自带的 SimpleMove() 方法实现移动
        characterController.SimpleMove(new Vector3(h, 0, v) * speed);
        //transform.position += new Vector3(h, 0, v) * speed * Time.deltaTime;
        Vector3 input = (transform.right * h + transform.up * v).normalized;
        if (h != 0 || v != 0)
        {
    
    
            Rotating(h, v);
        }
        if (input != Vector3.zero)
        {
    
    
            animator.SetBool("isMoving", true);
        }
        else
        {
    
    
            animator.SetBool("isMoving", false);
            //agent.enabled = true;
        }



    }

    void Rotating(float h, float v)
    {
    
    
        Vector3 targetDir = new Vector3(h, 0, v);

        // 传入一个向量值使物体朝向向量方向
        Quaternion targetRotation = Quaternion.LookRotation(targetDir, Vector3.up);

        transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);

    }
    
}

2. The camera moves with the movement of the characters

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

public class RotatingCamera : MonoBehaviour
{
    
    
    private Transform player;
    private bool isRotating = false;
    void Start()
    {
    
    
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    void Update()
    {
    
    
        transform.position = player.position;

    }

}

Guess you like

Origin blog.csdn.net/crush_oo/article/details/130271269