《游戏-03_3D-开发》之—新输入系统人物移动攻击连击

2024-01-26 18:36

本文主要是介绍《游戏-03_3D-开发》之—新输入系统人物移动攻击连击,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本次修改unity的新输入输出系统。本次修改unity需要重启,请先保存项目,

点击加号起名为MyCtrl,

点击加号设置为一轴的,

继续设置W键,

保存

生成自动脚本,

修改MyPlayer代码:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
    }
    private void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    private void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play)
            return;
        Anim.SetBool("IsRun", true);
    }
}

运行即可实现按键盘w/s键实现跑步松开即停止,

但只能实现动画,不能移动位置,

接下来添加跳跃:

修改MyPlayer代码:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
}

运行即可实现按键盘 空格 键实现跳跃,

但只能实现动画,

接下来设置旋转,

修改MyPlayer代码:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
}

继续添加新输入系统:


修改MyPlayer代码:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
    }
    void Hold(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if(obj.phase == InputActionPhase.Canceled)
            isHold = false;
        else
            isHold = true;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
}
接下来设置速度,

修改MyPlayer代码:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
    }
    void FastSpeed(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B")){
            if (obj.phase == InputActionPhase.Canceled)
                spdFast = 1;
            else
                spdFast = 0;
        }
    }
    void Hold(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if(obj.phase == InputActionPhase.Canceled)
            isHold = false;
        else
            isHold = true;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
}
设置获取道具,

修改MyPlayer代码:

using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
        action.MyCtrl.GetTool.started += ClickNpcAndTool;
    }
    void ClickNpcAndTool(InputAction.CallbackContext obj){
        //后期拓展
    }

    void FastSpeed(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B")){
            if (obj.phase == InputActionPhase.Canceled)
                spdFast = 1;
            else
                spdFast = 0;
        }
    }
    void Hold(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if(obj.phase == InputActionPhase.Canceled)
            isHold = false;
        else
            isHold = true;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
}
修改MyPlayer代码:

--------------------------------------------------【添加移动效果】-------------------------------------------------------

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        SetInput();
        //添加角色控制器
        contro = GetComponent<CharacterController>();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
        action.MyCtrl.GetTool.started += ClickNpcAndTool;
    }
    void ClickNpcAndTool(InputAction.CallbackContext obj){
        //后期拓展
    }
    void FastSpeed(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B")){
            if (obj.phase == InputActionPhase.Canceled)
                spdFast = 1;
            else
                spdFast = 0;
        }
    }
    void Hold(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if(obj.phase == InputActionPhase.Canceled)
            isHold = false;
        else
            isHold = true;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
    void Ctrl() {
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight")) {
            float f = action.MyCtrl.Move.ReadValue<float>();
            contro.Move(transform.forward * f * Time.deltaTime * spdFast * Spd);
            contro.Move(transform.up * -9.8f * Time.deltaTime);
            if (isHold)
                transform.Rotate(transform.up * rvalue * 0.3f);
        }
    }
    void Update(){
        if (GameManager.gameState != GameState.Play)
            return;
        Ctrl();
    }
}

在unity场景中对Player添加角色控制器,

运行即可实现移动,

设置拔剑,

设置攻击,

修改MyPlayer代码:

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包
    CharacterController contro;
    Controls action;
    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start(){
        base.Start();
        //添加角色控制器
        contro = GetComponent<CharacterController>();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
        action.MyCtrl.GetTool.started += ClickNpcAndTool;
        action.MyAtt.SwordOut.started += SwordOut;
        action.MyAtt.Att.started += Attack;
    }
    void Attack(InputAction.CallbackContext obj){
        //if (GameManager.gameState != GameState.Play)
        //    return;
        //if (EventSystem.current.IsPointerOverGameObject())
        //    return;
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight")){
            Anim.SetInteger("AttackID", 1);
            Anim.SetTrigger("AttackTrigger");
        }
        else {
            int num = Anim.GetInteger("AttackID");
            if (num == 6)
                return;
            if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Attack_" + num))
                Anim.SetInteger("AttackID", num + 1);
        }
    }
    public void PlayerAttack(string hurt) {
        Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
            attPoint.rotation, LayerMask.GetMask("Enemy"));
        if (cs.Length <= 0)
            return;
        int value = (int)(Att * Anim.GetInteger("AttackID") * 0.5f);
        foreach (Collider c in cs) {
            print(value);
        }
    }
    void SwordOut(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        Anim.SetBool("IsSwordOut",!Anim.GetBool("IsSwordOut"));
    }
    void ClickNpcAndTool(InputAction.CallbackContext obj){
        //后期拓展
    }
    void FastSpeed(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B")){
            if (obj.phase == InputActionPhase.Canceled)
                spdFast = 1;
            else
                spdFast = 0;
        }
    }
    void Hold(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        if(obj.phase == InputActionPhase.Canceled)
            isHold = false;
        else
            isHold = true;
    }
    void Rotate(InputAction.CallbackContext obj){
        if (GameManager.gameState != GameState.Play)
            return;
        rvalue = obj.ReadValue<float>();
    }
    void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("JumpTrigger");
    }
    void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play){
            return;
        }
        Anim.SetBool("IsRun", true);
    }
    void Ctrl() {
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_ver_B") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_ver_A") ||
           Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight")) {
            float f = action.MyCtrl.Move.ReadValue<float>();
            contro.Move(transform.forward * f * Time.deltaTime * spdFast * Spd);
            contro.Move(transform.up * -9.8f * Time.deltaTime);
            if (isHold)
                transform.Rotate(transform.up * rvalue * 0.3f);
        }
    }
    void Update(){
        if (GameManager.gameState != GameState.Play)
            return;
        Ctrl();
    }
}

运行及实现,

w/s键控制前行后退,空格跳跃,鼠标右键转动视角,按E键进入战斗状态,可以进行攻击,左键连续点击实现连击效果,再按E键进入非战斗状态,不能进行攻击,

End.

这篇关于《游戏-03_3D-开发》之—新输入系统人物移动攻击连击的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/647697

相关文章

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

Ubuntu系统怎么安装Warp? 新一代AI 终端神器安装使用方法

《Ubuntu系统怎么安装Warp?新一代AI终端神器安装使用方法》Warp是一款使用Rust开发的现代化AI终端工具,该怎么再Ubuntu系统中安装使用呢?下面我们就来看看详细教程... Warp Terminal 是一款使用 Rust 开发的现代化「AI 终端」工具。最初它只支持 MACOS,但在 20

windows系统下shutdown重启关机命令超详细教程

《windows系统下shutdown重启关机命令超详细教程》shutdown命令是一个强大的工具,允许你通过命令行快速完成关机、重启或注销操作,本文将为你详细解析shutdown命令的使用方法,并提... 目录一、shutdown 命令简介二、shutdown 命令的基本用法三、远程关机与重启四、实际应用