本次修改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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
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();
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();
}
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();
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.