/// /// ZWN /// 跳和跑代码 /// 20210917 /// using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum action_run_state { None, Jump, Run } public class action_run : MonoBehaviour { public static action_run single; public float space_time = 0.5f;//状态改变间隔时间 public bool Run_Flag = false;//整体是否检测 public float space_X = 0.5f;//跳跃阈值 public float spacehand = 0.5f;//跑步阈值 Dictionary zwn_Single_Vec = new Dictionary();//关于位置数据的存储 Vector3 hand_Right, Shoulder_Right, hand_Left, Shoulder_Left;//右手//右肘//右肩//左手//左肘//左肩 int hand_Count = 6, buff_sz = 3; //存储帧数 [HideInInspector] public action_run_state cur_RunState = action_run_state.None;//当前状态 [HideInInspector] public action_run_state last_RunState = action_run_state.None;//上状态记录 public float width;//宽 public List list_Shoulder_Y, list_Shoulder_X;//肩部XY记录 int jump_num, run_num, none_num;//状态数量统计 public Text show_Num;//状态显示 string last_state;//上个状态 Vector3 default_vec = new Vector3(-1, -1, 0); //关于左侧属性 #region bool flag_time_Left = false; float state_time_Left = 0; public List list_Hand_X_Left, list_Hand_Y_Left;//左右手X Y列表 #endregion //关于右侧属性 #region bool flag_time_Right = false; float state_time_Right = 0; public List list_Hand_X_Right, list_Hand_Y_Right;//左右手X Y列表 #endregion void Awake() { space_X = 1f;//跳跃阈值 spacehand = 1.5f;//跑步阈值 single = this; if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) { hand_Count = 25; } if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟 { hand_Count = 6; } //Init_Run(); } bool reInit = false; /// /// 初始化判断 /// public void Init_Run() { time_vec = 0; cur_RunState = action_run_state.None; flag_time_Left = true; state_time_Left = 0; flag_time_Right = true; state_time_Right = 0; if (reInit) { clear(); } Run_Flag = true; if (!reInit) { reInit = true; } } /// /// 结束判断 /// public void End_Run() { if (reInit) { clear(); } cur_RunState = action_run_state.None; flag_time_Left = true; state_time_Left = 0; flag_time_Right = true; state_time_Right = 0; Run_Flag = false; } void Update() { if (Run_Flag) { ZWN_Single_Vec(); List_Run(); if (show_Num != null) { show_Num.text = last_state + "跳:" + jump_num + "\n跑:" + run_num + "\n空:" + none_num; } } } void List_Run() { if ( Shoulder_Right.x != -1 && Shoulder_Right.y != -1 && Shoulder_Left.x != -1 && Shoulder_Left.y != -1) { list_Shoulder_Y.Add((int)((Shoulder_Right.y + Shoulder_Left.y) / 2)); list_Shoulder_X.Add((int)(Mathf.Abs(Shoulder_Right.x - Shoulder_Left.x) / 2)); } if (list_Shoulder_Y.Count > hand_Count) { list_Shoulder_Y.Remove(list_Shoulder_Y[0]); list_Shoulder_X.Remove(list_Shoulder_X[0]); List_Run_Y(); List_Run_X(); ratioTmpY = (max_RunY - min_RunY) / width;//肩部Y占比例 ratioTmpX = ave_List_RunX / width;//肩部X占比例 /* print("跳"+ cur_DirY +" "+ (cur_DirY > 0) + "\nratioTmpY:" + ratioTmpY + "\nratioTmpX:"+ratioTmpX+" "+ ratioTmpX * space_X + (ratioTmpY > ratioTmpX * space_X));*/ if (cur_DirY > 0 && ratioTmpY > ratioTmpX * space_X) { State_Jump(); } else { List_Run_Left(); } } } float cur_DirY = 0, cur_Dir_StartY = 0, cur_Dir_EndY = 0, max_RunY = -0xffff, min_RunY = 0xffff; float ratioTmpY; /// /// 求X位移差、位移比例、滑动方向 /// void List_Run_Y() { cur_DirY = 0; cur_Dir_StartY = 0; cur_Dir_EndY = 0; max_RunY = -0xffff; min_RunY = 0xffff; for (int i = 0; i < list_Shoulder_Y.Count; i++) { if (i == 0) { max_RunY = list_Shoulder_Y[i]; min_RunY = list_Shoulder_Y[i]; } if (i < buff_sz) { cur_Dir_StartY += list_Shoulder_Y[i]; } if (i >= list_Shoulder_Y.Count - buff_sz) { cur_Dir_EndY += list_Shoulder_Y[i]; } if (list_Shoulder_Y[i] > max_RunY) max_RunY = list_Shoulder_Y[i]; if (list_Shoulder_Y[i] < min_RunY) min_RunY = list_Shoulder_Y[i]; } cur_DirY = (cur_Dir_StartY / buff_sz) - (cur_Dir_EndY / buff_sz); } float cur_DirX = 0, cur_Dir_StartX = 0, cur_Dir_EndX = 0, max_RunX = -0xffff, min_RunX = 0xffff; float ave_List_RunX, ratioTmpX; /// /// 求X位移差、位移比例、滑动方向 /// void List_Run_X() { cur_DirX = 0; cur_Dir_StartX = 0; cur_Dir_EndX = 0; max_RunX = -0xffff; min_RunX = 0xffff; ave_List_RunX = 0; for (int i = 0; i < list_Shoulder_X.Count; i++) { if (i == 0) { max_RunX = list_Shoulder_X[i]; min_RunX = list_Shoulder_X[i]; } if (i < buff_sz) { cur_Dir_StartX += list_Shoulder_X[i]; } if (i >= list_Shoulder_X.Count - buff_sz) { cur_Dir_EndX += list_Shoulder_X[i]; } if (list_Shoulder_X[i] > max_RunY) max_RunX = list_Shoulder_X[i]; if (list_Shoulder_X[i] < min_RunY) min_RunX = list_Shoulder_X[i]; ave_List_RunX += list_Shoulder_X[i]; } ave_List_RunX = ave_List_RunX / list_Shoulder_X.Count; cur_DirX = (cur_Dir_StartX / buff_sz) - (cur_Dir_EndX / buff_sz); } public float time_vec = 0; /// /// 点信息 /// void ZWN_Single_Vec() { for (int i = 0; i < 17; i++) { if (zwn_Single_Vec.ContainsKey(i)) { zwn_Single_Vec[i] = zwn_common_data.single.zwn_original_pose[i]; } else { zwn_Single_Vec.Add(i, zwn_common_data.single.zwn_original_pose[i]); } } if (zwn_Single_Vec[9] == default_vec || zwn_Single_Vec[5] == default_vec || zwn_Single_Vec[10] == default_vec || zwn_Single_Vec[6] == default_vec) { time_vec += Time.deltaTime; if (time_vec >= 0.2f) { clear(); State_None(); time_vec = 0; } } else { time_vec = 0; } if (zwn_Single_Vec[9] != default_vec) { hand_Right = zwn_Single_Vec[9]; } if (zwn_Single_Vec[5] != default_vec) { Shoulder_Right = zwn_Single_Vec[5]; } if (zwn_Single_Vec[10] != default_vec) { hand_Left = zwn_Single_Vec[10]; } if (zwn_Single_Vec[6] != default_vec) { Shoulder_Left = zwn_Single_Vec[6]; } //if (width != Helper.Width) // width = Helper.Width; } /// /// 右侧状态判断 /// void List_Run_Right() { if (hand_Right.x != -1 && hand_Right.y != -1) { list_Hand_X_Right.Add(hand_Right.x); list_Hand_Y_Right.Add(hand_Right.y); } if (!flag_time_Right) { state_time_Right += Time.deltaTime; if (state_time_Right >= space_time) { flag_time_Right = true; state_time_Right = 0; } } if (list_Hand_X_Right.Count > hand_Count) { list_Hand_X_Right.Remove(list_Hand_X_Right[0]); list_Hand_Y_Right.Remove(list_Hand_Y_Right[0]); List_Run_Right_X(); List_Run_Right_Y(); ratioTmpX_Right = ave_X_Right / width;//右手整体位移X ratioTmpY_Right = ave_Y_Right / width;//右手整体位移Y //print("右手跑" + (ratioTmpX_Right > ratioTmpX * spacehand) + " " + (ratioTmpY_Right > ratioTmpX * spacehand)); if (ratioTmpX_Right > ratioTmpX * spacehand || ratioTmpY_Right > ratioTmpX * spacehand ) { //print("右"); State_Run(); } else { State_None(); } } } float ave_X_Right; float ratioTmpX_Right; /// /// 求X位移差、位移比例、滑动方向 /// void List_Run_Right_X() { ave_X_Right = 0; for (int i = 0; i < list_Hand_X_Right.Count; i++) { if (i + 1 < list_Hand_X_Right.Count) { ave_X_Right += Mathf.Abs(list_Hand_X_Right[i + 1] - list_Hand_X_Right[i]); } } } float ave_Y_Right; float ratioTmpY_Right; /// /// 求Y位移差与位移比例 /// void List_Run_Right_Y() { ave_Y_Right = 0; for (int i = 0; i < list_Hand_Y_Right.Count; i++) { if (i + 1 < list_Hand_Y_Right.Count) { ave_Y_Right += Mathf.Abs(list_Hand_Y_Right[i + 1] - list_Hand_Y_Right[i]); } } } /// /// 左侧状态判断 /// void List_Run_Left() { //print("hand_Left.x" + hand_Left.x + " hand_Left.y" + hand_Left.y); if (hand_Left.x != -1 && hand_Left.y != -1) { list_Hand_X_Left.Add(hand_Left.x); list_Hand_Y_Left.Add(hand_Left.y); } if (!flag_time_Left) { state_time_Left += Time.deltaTime; if (state_time_Left >= space_time) { flag_time_Left = true; state_time_Left = 0; } } if (list_Hand_X_Left.Count > hand_Count) { list_Hand_X_Left.Remove(list_Hand_X_Left[0]); list_Hand_Y_Left.Remove(list_Hand_Y_Left[0]); List_Run_Left_X(); List_Run_Left_Y(); ratioTmpX_Left = ave_X_Left / width;//右手整体位移X ratioTmpY_Left = ave_Y_Left / width;//右手整体位移Y //print("左手跑" + (ratioTmpX_Left > ratioTmpX * spacehand) + " " + (ratioTmpY_Left > ratioTmpX * spacehand) + //" " + ratioTmpX_Left + " " + ratioTmpX * spacehand + " " + ratioTmpY_Left); if (ratioTmpX_Left > ratioTmpX * spacehand || ratioTmpY_Left > ratioTmpX * spacehand ) { //print("左"); State_Run(); } else { //State_None(); List_Run_Right(); } } } float ave_X_Left; float ratioTmpX_Left; /// /// 求X位移差、位移比例、滑动方向 /// void List_Run_Left_X() { ave_X_Left = 0; for (int i = 0; i < list_Hand_X_Left.Count; i++) { if (i + 1 < list_Hand_X_Left.Count) { ave_X_Left += Mathf.Abs(list_Hand_X_Left[i + 1] - list_Hand_X_Left[i]); } } } float ave_Y_Left; float ratioTmpY_Left; /// /// 求Y位移差与位移比例 /// void List_Run_Left_Y() { ave_Y_Left = 0; for (int i = 0; i < list_Hand_Y_Left.Count; i++) { if (i + 1 < list_Hand_Y_Left.Count) { ave_Y_Left += Mathf.Abs(list_Hand_Y_Left[i + 1] - list_Hand_Y_Left[i]); } } } /// /// 跳跃状态 /// void State_Jump() { if (cur_RunState != action_run_state.Jump) { jump_num += 1; last_State(); cur_RunState = action_run_state.Jump; last_RunState = cur_RunState; } } /// /// 跑步状态 /// void State_Run() { /* if (cur_RunState != action_run_state.Run) {*/ run_num += 1; cur_RunState = action_run_state.Run; last_State(); last_RunState = cur_RunState; state_time_Right = 0; /* }*/ } /// /// 空状态 /// void State_None() { /* if (cur_RunState != action_run_state.None) {*/ none_num += 1; cur_RunState = action_run_state.None; last_State(); last_RunState = cur_RunState; //clear(); /* }*/ } /// /// 上一个状态提示 /// void last_State() { last_state = "上一个状态:"; switch (last_RunState) { case action_run_state.None: last_state += "空\n"; break; case action_run_state.Jump: last_state += "跳\n"; break; case action_run_state.Run: last_state += "跑\n"; break; } switch (cur_RunState) { case action_run_state.None: last_state += "状态空\n"; break; case action_run_state.Jump: last_state += "状态跳\n"; break; case action_run_state.Run: last_state += "状态跑\n"; break; } } /// /// 将存储数据清空 /// void clear() { if (list_Hand_X_Right.Count > 0) { list_Hand_Y_Right.Clear(); list_Hand_X_Right.Clear(); } if (list_Hand_X_Left.Count > 0) { list_Hand_Y_Left.Clear(); list_Hand_X_Left.Clear(); } if (list_Shoulder_X.Count > 0) { list_Shoulder_X.Clear(); list_Shoulder_Y.Clear(); } } }