浏览代码

add heuristic ai behavior

/hh-develop-fps_game_project
HH 4 年前
当前提交
34959fa9
共有 7 个文件被更改,包括 144 次插入3 次删除
  1. 36
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentCubeMovement.cs
  2. 12
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentHealth.cs
  3. 4
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/GameController.cs
  4. 2
      Project/ProjectSettings/TagManager.asset
  5. 82
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/PlayerAIHeuristic.cs
  6. 11
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/PlayerAIHeuristic.cs.meta

36
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentCubeMovement.cs


dashCoolDownTimer = 0;
}
}
public void RotateTowards(Vector3 dir, float maxRotationRate = 1)
{
if (dir != Vector3.zero)
{
var rot = Quaternion.LookRotation(dir);
var smoothedRot = Quaternion.RotateTowards(rb.rotation, rot, maxRotationRate * Time.deltaTime);
rb.MoveRotation(smoothedRot);
}
}
public void RunOnGround(Vector3 dir)
{
if (dir == Vector3.zero)
{
if (AnimateBodyMesh)
{
bodyMesh.localPosition = Vector3.zero;
}
}
else
{
var vel = rb.velocity.magnitude;
float adjustedSpeed = Mathf.Clamp(agentRunSpeed - vel, 0, agentTerminalVel);
// rb.AddForce(dir.normalized * adjustedSpeed, runningForceMode);
rb.AddForce(dir * adjustedSpeed, runningForceMode);
if (AnimateBodyMesh)
{
bodyMesh.localPosition = Vector3.zero +
Vector3.up * walkingAnimScale * walkingBounceCurve.Evaluate(
m_animateBodyMeshCurveTimer);
m_animateBodyMeshCurveTimer += Time.fixedDeltaTime;
}
// rb.AddForceAtPosition(dir.normalized * adjustedSpeed,transform.TransformPoint(Vector3.forward * standingForcePositionOffset),
// runningForceMode);
}
}
public void RunOnGround(Rigidbody rb, Vector3 dir)
{
if (dir == Vector3.zero)

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


public class AgentHealth : MonoBehaviour
{
public float CurrentPercentage = 100;
public float DepletionRate = 5f; //constant rate at which ammo depletes when being used
public Slider UISlider;
public MeshRenderer bodyMesh;

public bool Dead;
private GameController GameController;
[Header("PLAYER DAMAGE")] public bool UseGlobalDamageSettings;
public float DamagePerHit = 15f; //constant rate at which ammo depletes when being used
// Start is called before the first frame update
void OnEnable()
{

{
return;
}
CurrentPercentage = Mathf.Clamp(CurrentPercentage - DepletionRate, 0, 100);
var damage = DamagePerHit;
if (UseGlobalDamageSettings)
{
damage = GameController.DamagePerHit;
}
CurrentPercentage = Mathf.Clamp(CurrentPercentage - damage, 0, 100);
if (CurrentPercentage == 0)
{
Dead = true;

4
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/GameController.cs


public class GameController : MonoBehaviour
{
[Header("GLOBAL SETTINGS")]
public List<Rigidbody> AllRBsList = new List<Rigidbody>();
public float ExplosionForce = 100;

public bool triggerExplosion;
[Header("PLAYER DAMAGE")]
public float DamagePerHit = 15;
// Start is called before the first frame update
void Awake()
{

2
Project/ProjectSettings/TagManager.asset


- invisible
- shield
- projectile
-
- agent
-
-
-

82
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/PlayerAIHeuristic.cs


using System.Collections;
using System.Collections.Generic;
using MLAgents;
using UnityEngine;
public class PlayerAIHeuristic : MonoBehaviour
{
[Header("TARGET")]
public Transform target;
[Header("VISION")] public bool canCurrentlySeeTarget;
[Header("WALKING")]
public bool RunTowardsTarget;
public bool OnlyWalkIfCanSeeTarget = true;
[Header("BODY ROTATION")]
public float MaxRotationRate = 1;
public float RandomRotationJitter = 1.5f;
// public LayerMask AgentLayer;
public string TargetTag = "agent";
public bool RotateTowardsTarget;
[Header("SHOOTING")]
public bool OnlyShootIfCanSeeTarget = true;
private AgentCubeMovement moveController;
private AgentHealth agentHealth;
private MultiGunAlternating multiGunController;
// Start is called before the first frame update
void Start()
{
moveController = GetComponent<AgentCubeMovement>();
agentHealth = GetComponent<AgentHealth>();
multiGunController = GetComponent<MultiGunAlternating>();
}
void Update()
{
canCurrentlySeeTarget = false;
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 50))
{
if (hit.transform == target) //simple vision
{
canCurrentlySeeTarget = true;
}
}
}
// Update is called once per frame
void FixedUpdate()
{
if (agentHealth && agentHealth.Dead)
{
return;
}
Vector3 randomJitter = Random.insideUnitSphere * RandomRotationJitter;
randomJitter.y = 0;
Vector3 targetPos = target.position + randomJitter;
// Vector3 dir = target.position - transform.position;
Vector3 dir = targetPos - transform.position;
if (RunTowardsTarget)
{
if (OnlyWalkIfCanSeeTarget && canCurrentlySeeTarget)
{
moveController.RunOnGround(dir.normalized);
}
}
if (RotateTowardsTarget)
{
moveController.RotateTowards(dir.normalized, MaxRotationRate);
}
if (OnlyShootIfCanSeeTarget && canCurrentlySeeTarget && multiGunController)
{
multiGunController.Shoot();
}
}
}

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


fileFormatVersion: 2
guid: ceeb907ca54e84d77b6d31c0a264e386
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存