Unity C# 角色控制脚本:使用键盘WSAD控制角色移动和技能

本文将介绍如何将Unity C# 角色控制脚本从使用虚拟摇杆改为使用键盘WSAD控制角色移动和技能。

原始代码:

public class CharacterInputController : MonoBehaviour
{
    private ETCJoystick joystick;
    private CharacterMotor chMotor;
    private Animator anim;
    private PlayerStatus status;
    private ETCButton[] skillButtons;
    private CharacterSkillSystem skillSystem;

    private void Awake()
    {
        //查找组件
        joystick = FindObjectOfType<ETCJoystick>();
        chMotor = GetComponent<CharacterMotor>();
        anim = GetComponentInChildren<Animator>();
        status = GetComponent<PlayerStatus>();
        skillButtons = FindObjectsOfType<ETCButton>();
        skillSystem = GetComponent<CharacterSkillSystem>();
    }

    private void OnEnable()
    {
        //注册事件
        joystick.onMove.AddListener(OnJoystickMove);
        joystick.onMoveStart.AddListener(OnJoystickMoveStart);
        joystick.onMoveEnd.AddListener(OnJoystickMoveEnd);

        for (int i = 0; i < skillButtons.Length; i++)
        {
            if (skillButtons[i].name == 'BaseButton')
                skillButtons[i].onPressed.AddListener(OnSkillButtonPressed);
            else
                skillButtons[i].onDown.AddListener(OnSkillButtonDown);
        }
    }

    private float lastPressTime = -1;
    //当按住普攻键时执行
    private void OnSkillButtonPressed()
    {
        if (IsAttacking()) return;

        //按住间隔如果过小(2) 则取消攻击
        //间隔小于5秒视为连击
        //间隔:当前按下时间  -  最后按下时间
        float interval = Time.time - lastPressTime;
        if (interval < 2) return;
        bool isBatter = interval <= 5;
        //if (interval <= 5)
        //    isBatter = true;
        //else
        //    isBatter = false;

        skillSystem.AttackUseSkill(1001, isBatter);

        lastPressTime = Time.time;
    }

    //多个按钮  绑定一个方法。
    //需要通过事件参数类,区分点击的按钮。
    private void OnSkillButtonDown(string name)
    {
        //如果正在攻击 则退出
        if (IsAttacking()) return;

        int id = 0;
        switch (name)
        {
            //case 'BaseButton':
            //    id = 1001;
            //    break;
            case 'SkillButton01':
                id = 1002;
                break;
            case 'SkillButton02':
                id = 1003;
                break;
        }
        //CharacterSkillManager skillManager = GetComponent<CharacterSkillManager>();
        ////准备技能(判断条件)
        //SkillData data = skillManager.PrepareSkill(id);
        //if (data != null)//生成技能
        //    skillManager.GenerateSkill(data);
        skillSystem.AttackUseSkill(id);
    }

    private void OnDisable()
    {
        //注销事件
        joystick.onMove.RemoveListener(OnJoystickMove);
        joystick.onMoveStart.RemoveListener(OnJoystickMoveStart);
        joystick.onMoveEnd.RemoveListener(OnJoystickMoveEnd);

        for (int i = 0; i < skillButtons.Length; i++)
        {
            if (skillButtons[i] == null) continue;
            if (skillButtons[i].name == 'BaseButton')
                skillButtons[i].onPressed.RemoveListener(OnSkillButtonPressed);
            else
                skillButtons[i].onDown.RemoveListener(OnSkillButtonDown);
        }
    }

    private void OnJoystickMoveStart()
    {
        anim.SetBool(status.chParams.run, true);
    }

    private void OnJoystickMoveEnd()
    {
        anim.SetBool(status.chParams.run, false);
    }

    private void OnJoystickMove(Vector2 dir)
    {
        //调用马达移动功能
        //dir.x       左右           0              dir.y       上下
        //  x                           y                      z
        chMotor.Movement(new Vector3(dir.x, 0, dir.y));
    }

    private bool IsAttacking()
    {
        return anim.GetBool(status.chParams.attack01) || anim.GetBool(status.chParams.attack02) || anim.GetBool(status.chParams.attack03);
    }
}

修改后的代码:

public class CharacterInputController : MonoBehaviour
{
    private CharacterMotor chMotor;
    private Animator anim;
    private PlayerStatus status;
    private CharacterSkillSystem skillSystem;

    private void Awake()
    {
        chMotor = GetComponent<CharacterMotor>();
        anim = GetComponentInChildren<Animator>();
        status = GetComponent<PlayerStatus>();
        skillSystem = GetComponent<CharacterSkillSystem>();
    }

    private void Update()
    {
        // 检测WSAD按键输入
        float horizontalInput = Input.GetAxis('Horizontal');
        float verticalInput = Input.GetAxis('Vertical');

        // 调用马达移动功能
        chMotor.Movement(new Vector3(horizontalInput, 0, verticalInput));

        // 检测普攻键按下
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (IsAttacking()) return;

            float interval = Time.time - lastPressTime;
            if (interval < 2) return;
            bool isBatter = interval <= 5;

            skillSystem.AttackUseSkill(1001, isBatter);

            lastPressTime = Time.time;
        }

        // 检测技能键按下
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            if (IsAttacking()) return;
            skillSystem.AttackUseSkill(1002);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            if (IsAttacking()) return;
            skillSystem.AttackUseSkill(1003);
        }
    }

    private float lastPressTime = -1;

    private bool IsAttacking()
    {
        return anim.GetBool(status.chParams.attack01) || anim.GetBool(status.chParams.attack02) || anim.GetBool(status.chParams.attack03);
    }
}

代码解释:

  1. 删除虚拟摇杆相关代码: 删除与ETCJoystick和ETCButton相关的代码,因为不再使用虚拟摇杆进行控制。
  2. 使用Input.GetAxis检测键盘输入: 使用Input.GetAxis('Horizontal')和Input.GetAxis('Vertical')来获取水平和垂直方向的键盘输入值。
  3. 使用Input.GetKeyDown检测技能键按下: 使用Input.GetKeyDown来检测Space键(普攻)和数字键1和2(技能)的按下,并根据按键的不同调用相应的技能函数。

注意:

  • 该代码示例使用了Unity中的Input类来检测键盘输入,您需要确保您的项目中已包含该类。
  • 您可能需要根据您的游戏逻辑和技能系统进行相应的修改。
  • 为了方便理解,代码示例中使用了简单的技能ID,实际应用中您可能需要使用更复杂的技能系统。

通过以上步骤,您就可以将Unity C# 角色控制脚本从使用虚拟摇杆改为使用键盘WSAD控制角色移动和技能。

Unity C# 角色控制脚本:使用键盘WSAD控制角色移动和技能

原文地址: https://www.cveoy.top/t/topic/cqLa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录