HH
4 年前
当前提交
34959fa9
共有 7 个文件被更改,包括 144 次插入 和 3 次删除
-
36Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentCubeMovement.cs
-
12Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/AgentHealth.cs
-
4Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/GameController.cs
-
2Project/ProjectSettings/TagManager.asset
-
82Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/PlayerAIHeuristic.cs
-
11Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/PlayerAIHeuristic.cs.meta
|
|||
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(); |
|||
} |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ceeb907ca54e84d77b6d31c0a264e386 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
部分文件因为文件数量过多而无法显示
撰写
预览
正在加载...
取消
保存
Reference in new issue