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

相关推荐

  1. Unity3D开发鼠标单双判断

    2024-01-26 08:18:01       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-26 08:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-26 08:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-26 08:18:01       82 阅读
  4. Python语言-面向对象

    2024-01-26 08:18:01       91 阅读

热门阅读

  1. 苹果笔记本(MAC)常用快捷键

    2024-01-26 08:18:01       49 阅读
  2. SpringBoot引入缓存提高单次查询数据效率

    2024-01-26 08:18:01       48 阅读
  3. sql查看指定时间段的数据

    2024-01-26 08:18:01       49 阅读
  4. C#凹多边形求内心

    2024-01-26 08:18:01       55 阅读
  5. CGAL内置的边塌陷算法代码解析

    2024-01-26 08:18:01       45 阅读
  6. 关闭windows自动更新的6种方法

    2024-01-26 08:18:01       52 阅读
  7. 头歌C++之if-else基本应用

    2024-01-26 08:18:01       48 阅读
  8. 深度学习-Pytorch如何构建和训练模型

    2024-01-26 08:18:01       52 阅读
  9. VSCode连接远程服务器时无法下载或上传vscode-server

    2024-01-26 08:18:01       76 阅读
  10. 常见 BUG 问题面试系列-01

    2024-01-26 08:18:01       30 阅读