《游戏-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

相关文章

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis