浏览代码

fixed input not triggering properly + misc jank

/hh-develop-fps_game_project
HH 4 年前
当前提交
87b99a85
共有 10 个文件被更改,包括 369 次插入30 次删除
  1. 53
      Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSAgentInput.cs
  2. 134
      Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSPlayerInputActions.cs
  3. 124
      Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSPlayerInputActions.inputactions
  4. 4
      Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/Projectile.prefab
  5. 12
      Project/Assets/ML-Agents/Examples/FPS_Game/Scenes/FPS_Game.unity
  6. 1
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentHealth.cs
  7. 21
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/FPSAgent.cs
  8. 2
      Project/Assets/ML-Agents/Examples/Soccer/Materials/ClearPlastic.mat
  9. 37
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ImpactFlash.cs
  10. 11
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ImpactFlash.cs.meta

53
Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSAgentInput.cs


using UnityEngine;
using UnityEngine.InputSystem;
//[ExecuteAlways]
public class FPSAgentInput : MonoBehaviour
{
public bool DisableInput = false;

public bool shootInput;
public bool jumpInput;
public bool dashInput;
public Vector2 rotateInput;
public float rotateInput;
public bool shieldInput;
// Start is called before the first frame update

inputActions.Disable();
}
public bool CheckIfInputSinceLastFrame(ref bool input)
{
if (input)
{
input = false;
return true;
}
return false;
}
// public bool JumpCheck(ref bool input)
// {
// if (jumped)
// {
// jumped = false;
// return true;
// }
// return false;
// }
// Update is called once per frame
void FixedUpdate()
{

return;
}
moveInput = actionMap.Walk.ReadValue<Vector2>();
// shootInput = actionMap.Shoot.ReadValue<float>() > 0;
shootInput = gamepad.rightTrigger.isPressed;
shieldInput = gamepad.leftTrigger.isPressed;
shootInput = actionMap.Shoot.ReadValue<float>() > 0;
// shootInput = gamepad.rightTrigger.isPressed;
shieldInput = actionMap.Shield.ReadValue<float>() > 0;
// rotateInput = actionMap.RotateBody.ReadValue<Vector2>();
rotateInput = actionMap.Rotate.ReadValue<float>();
rotateInput = actionMap.RotateBody.ReadValue<Vector2>();
// jumpInput = actionMap.Jump.performed;
jumpInput = gamepad.buttonSouth.isPressed;
dashInput = gamepad.buttonWest.isPressed;
// jumpInput = actionMap.Jump.triggered;
if (actionMap.Dash.triggered)
{
dashInput = true;
}
if (actionMap.Jump.triggered)
{
jumpInput = true;
}
// if (jumpInput)
// {
// print($"Input: Jump: {jumpInput} : {Time.frameCount}");
//
// }
// jumpInput = gamepad.buttonSouth.isPressed;
// dashInput = actionMap.Dash.ReadValue<float>() > 0;
// actionMap.Dash.phase == InputActionPhase.
// dashInput = gamepad.buttonWest.isPressed;
}
}

134
Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSPlayerInputActions.cs


""expectedControlType"": ""Button"",
""processors"": """",
""interactions"": """"
},
{
""name"": ""Rotate"",
""type"": ""Value"",
""id"": ""b309ca48-95aa-4032-bd7d-46dcaeb138d0"",
""expectedControlType"": ""Axis"",
""processors"": """",
""interactions"": """"
}
],
""bindings"": [

""path"": ""2DVector"",
""path"": ""2DVector(mode=2)"",
""interactions"": """",
""processors"": """",
""groups"": """",

{
""name"": """",
""id"": ""28ddf0bd-ea22-4069-ba60-ca41d4742bf9"",
""path"": ""<Gamepad>/buttonEast"",
""path"": ""<Gamepad>/buttonSouth"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Jump"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""6f4a5988-5e01-4c66-a3ab-f4b5e47bd21f"",
""path"": ""<Keyboard>/j"",
""interactions"": """",
""processors"": """",
""groups"": """",

{
""name"": """",
""id"": ""fe641861-0da7-43ba-8bd6-e95a17232a77"",
""path"": ""<Gamepad>/buttonSouth"",
""path"": ""<Gamepad>/rightTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Shoot"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""2ae82452-d746-4a28-86f4-43c469c78eff"",
""path"": ""<Keyboard>/k"",
""interactions"": """",
""processors"": """",
""groups"": """",

""name"": """",
""id"": ""2b94ae52-9bb0-478e-8e4c-bf6c747a5c7d"",
""path"": ""<Gamepad>/leftTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Shield"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""75b3d327-54da-4b56-a39e-052b1c173b56"",
""path"": ""<Keyboard>/i"",
""interactions"": """",
""processors"": """",
""groups"": """",

""action"": ""Dash"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""ae8dc5a5-930d-412e-a196-627e63bfcd0c"",
""path"": ""<Keyboard>/l"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Dash"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": ""1D Axis"",
""id"": ""3e2a8f42-5d52-42d2-bd25-2934ab0f8c53"",
""path"": ""1DAxis"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""negative"",
""id"": ""561ac5e2-b709-4ae7-8151-1777260a4798"",
""path"": ""<Keyboard>/q"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""positive"",
""id"": ""23905e8a-1906-4f34-8e8f-5e6fa596e27e"",
""path"": ""<Keyboard>/e"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""1D Axis"",
""id"": ""a788bd05-d81e-47f4-a14c-5cb071e21c96"",
""path"": ""1DAxis"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""negative"",
""id"": ""9844ff0b-6a74-43bb-a969-9f59c74212b9"",
""path"": ""<Gamepad>/rightStick/left"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""positive"",
""id"": ""b81e038b-8786-4bbe-bb1d-aba1d54112d1"",
""path"": ""<Gamepad>/rightStick/right"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Rotate"",
""isComposite"": false,
""isPartOfComposite"": true
}
]
}

m_PlayerActionMap_Shield = m_PlayerActionMap.FindAction("Shield", throwIfNotFound: true);
m_PlayerActionMap_RotateBody = m_PlayerActionMap.FindAction("RotateBody", throwIfNotFound: true);
m_PlayerActionMap_Dash = m_PlayerActionMap.FindAction("Dash", throwIfNotFound: true);
m_PlayerActionMap_Rotate = m_PlayerActionMap.FindAction("Rotate", throwIfNotFound: true);
}
public void Dispose()

private readonly InputAction m_PlayerActionMap_Shield;
private readonly InputAction m_PlayerActionMap_RotateBody;
private readonly InputAction m_PlayerActionMap_Dash;
private readonly InputAction m_PlayerActionMap_Rotate;
public struct PlayerActionMapActions
{
private @FPSPlayerInputActions m_Wrapper;

public InputAction @Shield => m_Wrapper.m_PlayerActionMap_Shield;
public InputAction @RotateBody => m_Wrapper.m_PlayerActionMap_RotateBody;
public InputAction @Dash => m_Wrapper.m_PlayerActionMap_Dash;
public InputAction @Rotate => m_Wrapper.m_PlayerActionMap_Rotate;
public InputActionMap Get() { return m_Wrapper.m_PlayerActionMap; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }

@Dash.started -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnDash;
@Dash.performed -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnDash;
@Dash.canceled -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnDash;
@Rotate.started -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnRotate;
@Rotate.performed -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnRotate;
@Rotate.canceled -= m_Wrapper.m_PlayerActionMapActionsCallbackInterface.OnRotate;
}
m_Wrapper.m_PlayerActionMapActionsCallbackInterface = instance;
if (instance != null)

@Dash.started += instance.OnDash;
@Dash.performed += instance.OnDash;
@Dash.canceled += instance.OnDash;
@Rotate.started += instance.OnRotate;
@Rotate.performed += instance.OnRotate;
@Rotate.canceled += instance.OnRotate;
}
}
}

void OnShield(InputAction.CallbackContext context);
void OnRotateBody(InputAction.CallbackContext context);
void OnDash(InputAction.CallbackContext context);
void OnRotate(InputAction.CallbackContext context);
}
}

124
Project/Assets/ML-Agents/Examples/FPS_Game/Input/FPSPlayerInputActions.inputactions


"expectedControlType": "Button",
"processors": "",
"interactions": ""
},
{
"name": "Rotate",
"type": "Value",
"id": "b309ca48-95aa-4032-bd7d-46dcaeb138d0",
"expectedControlType": "Axis",
"processors": "",
"interactions": ""
}
],
"bindings": [

"path": "2DVector",
"path": "2DVector(mode=2)",
"interactions": "",
"processors": "",
"groups": "",

{
"name": "",
"id": "28ddf0bd-ea22-4069-ba60-ca41d4742bf9",
"path": "<Gamepad>/buttonEast",
"path": "<Gamepad>/buttonSouth",
"interactions": "",
"processors": "",
"groups": "",
"action": "Jump",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "6f4a5988-5e01-4c66-a3ab-f4b5e47bd21f",
"path": "<Keyboard>/j",
"interactions": "",
"processors": "",
"groups": "",

{
"name": "",
"id": "fe641861-0da7-43ba-8bd6-e95a17232a77",
"path": "<Gamepad>/buttonSouth",
"path": "<Gamepad>/rightTrigger",
"interactions": "",
"processors": "",
"groups": "",
"action": "Shoot",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "2ae82452-d746-4a28-86f4-43c469c78eff",
"path": "<Keyboard>/k",
"interactions": "",
"processors": "",
"groups": "",

"name": "",
"id": "2b94ae52-9bb0-478e-8e4c-bf6c747a5c7d",
"path": "<Gamepad>/leftTrigger",
"interactions": "",
"processors": "",
"groups": "",
"action": "Shield",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "75b3d327-54da-4b56-a39e-052b1c173b56",
"path": "<Keyboard>/i",
"interactions": "",
"processors": "",
"groups": "",

"action": "Dash",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "ae8dc5a5-930d-412e-a196-627e63bfcd0c",
"path": "<Keyboard>/l",
"interactions": "",
"processors": "",
"groups": "",
"action": "Dash",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "1D Axis",
"id": "3e2a8f42-5d52-42d2-bd25-2934ab0f8c53",
"path": "1DAxis",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "negative",
"id": "561ac5e2-b709-4ae7-8151-1777260a4798",
"path": "<Keyboard>/q",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "positive",
"id": "23905e8a-1906-4f34-8e8f-5e6fa596e27e",
"path": "<Keyboard>/e",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "1D Axis",
"id": "a788bd05-d81e-47f4-a14c-5cb071e21c96",
"path": "1DAxis",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "negative",
"id": "9844ff0b-6a74-43bb-a969-9f59c74212b9",
"path": "<Gamepad>/rightStick/left",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "positive",
"id": "b81e038b-8786-4bbe-bb1d-aba1d54112d1",
"path": "<Gamepad>/rightStick/right",
"interactions": "",
"processors": "",
"groups": "",
"action": "Rotate",
"isComposite": false,
"isPartOfComposite": true
}
]
}

4
Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/Projectile.prefab


m_GameObject: {fileID: 1465564013992665965}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.51999, y: 0.51999, z: 0.51999}
m_LocalScale: {x: 0.92688626, y: 0.92688626, z: 0.92688626}
m_Children: []
m_Father: {fileID: 3911029810215854904}
m_RootOrder: 1

m_GameObject: {fileID: 3911029810215854908}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Enabled: 0
serializedVersion: 2
m_Radius: 0.5
m_Center: {x: 0, y: 0, z: 0}

12
Project/Assets/ML-Agents/Examples/FPS_Game/Scenes/FPS_Game.unity


shootInput: 0
jumpInput: 0
dashInput: 0
rotateInput: {x: 0, y: 0}
rotateInput: 0
shieldInput: 0
--- !u!114 &1967720264
MonoBehaviour:

m_GameObject: {fileID: 763385666001942670}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 3, y: 3, z: 3}
m_LocalScale: {x: 2.16, y: 2.16, z: 2.16}
m_Children: []
m_Father: {fileID: 1479187681323959701}
m_RootOrder: 2

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!33 &763385666001942674
MeshFilter:
m_ObjectHideFlags: 0

shootInput: 0
jumpInput: 0
dashInput: 0
rotateInput: {x: 0, y: 0}
rotateInput: 0
shieldInput: 0
--- !u!114 &1479187681323959776
MonoBehaviour:

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6815147846413621513}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.858}
m_LocalPosition: {x: 0, y: 0, z: 1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6815147846237266779}

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6815147846637603125}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.858}
m_LocalPosition: {x: 0, y: 0, z: 1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6815147846181201187}

1
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentHealth.cs


{
bodyMesh.material.color = startingColor;
}
}
}

21
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/FPSAgent.cs


contActionsOut[0] = input.moveInput.y;
contActionsOut[1] = input.moveInput.x;
contActionsOut[2] = input.rotateInput.x;
// contActionsOut[2] = input.rotateInput.x; //rotate
contActionsOut[2] = input.rotateInput; //rotate
contActionsOut[4] = input.jumpInput ? 1 : 0; //jump
contActionsOut[5] = input.dashInput ? 1 : 0; //dash
contActionsOut[4] = input.CheckIfInputSinceLastFrame(ref input.jumpInput) ? 1 : 0; //jump
contActionsOut[5] = input.CheckIfInputSinceLastFrame(ref input.dashInput) ? 1 : 0; //jump
// contActionsOut[4] = input.jumpInput ? 1 : 0; //jump
// contActionsOut[5] = input.dashInput ? 1 : 0; //dash
// contActionsOut[0] = inputMovement.y;
// contActionsOut[1] = inputMovement.x;
// contActionsOut[2] = rotateMovement.x;
if (input.jumpInput)
{
print($"Agent: Jump: {input.jumpInput} : {Time.frameCount}");
}
// contActionsOut[0] = inputMovement.y;
// contActionsOut[1] = inputMovement.x;
// contActionsOut[2] = rotateMovement.x;
// m_InputH = 0;

2
Project/Assets/ML-Agents/Examples/Soccer/Materials/ClearPlastic.mat


- _DetailNormalMapScale: 1
- _DstBlend: 10
- _GlossMapScale: 1
- _Glossiness: 0.724
- _Glossiness: 0.75
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 3

37
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ImpactFlash.cs


//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;
//
//public class ImpactFlash : MonoBehaviour
//{
// // Start is called before the first frame update
// void Start()
// {
//
// }
//
// // Update is called once per frame
// void Update()
// {
//
// }
//
// IEnumerator Flash()
// {
// WaitForFixedUpdate wait = new WaitForFixedUpdate();
// if (bodyMesh)
// {
// bodyMesh.material.color = damageColor;
// }
// float timer = 0;
// while (timer < damageFlashDuration)
// {
// timer += Time.fixedDeltaTime;
// yield return wait;
// }
// if (bodyMesh)
// {
// bodyMesh.material.color = startingColor;
// }
// }
//}

11
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ImpactFlash.cs.meta


fileFormatVersion: 2
guid: aa2c440c774cb4535a98f276068c77b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存