Skip to content

Commit

Permalink
edit: 1.10 task3 test1
Browse files Browse the repository at this point in the history
  • Loading branch information
colourfulspring committed Jan 17, 2024
1 parent 5cd42cd commit 8a2fcc2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ NavMeshAgent:
m_Enabled: 1
m_AgentTypeID: -1372625422
m_Radius: 37.5
m_Speed: 100
m_Acceleration: 500
m_Speed: 200
m_Acceleration: 1000
avoidancePriority: 50
m_AngularSpeed: 120
m_StoppingDistance: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ PrefabInstance:
- target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a,
type: 3}
propertyPath: customNavigationGoal.x
value: -212
value: 281
objectReference: {fileID: 0}
- target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a,
type: 3}
Expand All @@ -248,7 +248,7 @@ PrefabInstance:
- target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a,
type: 3}
propertyPath: customNavigationGoal.z
value: -231
value: -152
objectReference: {fileID: 0}
- target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a,
type: 3}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using UnityEngine;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
Expand All @@ -23,8 +22,12 @@ protected override void Awake()

void Start()
{
var goal = useRandomNavigationGoal ? customNavigationGoal : customNavigationGoal;
GetComponent<NavMeshAgent>().SetDestination(customNavigationGoal);
var localY = transform.localPosition.y; // Attention: local Y

var navigationGoal = useRandomNavigationGoal
? carCatchingEnvController.GetRandomPos() + new Vector3(0f, localY, 0f)
: customNavigationGoal;
GetComponent<NavMeshAgent>().SetDestination(navigationGoal);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,41 @@ public class CarInfo

// private int m_ResetTimer;

void Start()
protected void Awake()
{
// Get the ground's bounds
areaBounds = ground.GetComponent<Collider>().bounds;
m_CarCatchingSettings = FindObjectOfType<CarCatchingSettings>();
}

void Start()
{
foreach (var item in AgentsList)
{
item.StartingPos = item.Agent.transform.position;
item.StartingRot = item.Agent.transform.rotation;
item.StartingScale = item.Agent.transform.localScale;
item.GoalPosition = item.Agent.transform.position;
var itemTrans = item.Agent.transform;
item.StartingPos = itemTrans.position;
item.StartingRot = itemTrans.rotation;
item.StartingScale = itemTrans.localScale;
item.GoalPosition = itemTrans.position;
}

ResetScene();
}

/// <summary>
/// Use the ground's bounds to pick a random local position in the area bounds. Make sure the Y value
/// is 0, X and Z value lies in [-areaBounds.extents.x(z), areaBounds.extents.x(z)], respectively.
/// </summary>
public Vector3 GetRandomPos()
{
var randomPosX = Random.Range(-areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier,
areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier);

var randomPosZ = Random.Range(-areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier,
areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier);
return new Vector3(randomPosX, 0f, randomPosZ);
}

/// <summary>
/// Use the ground's bounds to pick a random spawn position. The requirements is that the car's collider
/// doesn't collide with any wall's collider
Expand All @@ -76,13 +95,8 @@ public Vector3 GetRandomSpawnPos(Quaternion rot)
var agentHalfExtents = AgentsList[0].StartingScale * 0.5f;
while (foundNewSpawnLocation == false)
{
var randomPosX = Random.Range(-areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier,
areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier);

var randomPosZ = Random.Range(-areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier,
areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier);
//Global Position
randomSpawnPos = ground.transform.position + new Vector3(randomPosX, localY, randomPosZ);
//Global Position = ground position + x,z local position + y
randomSpawnPos = ground.transform.position + GetRandomPos() + new Vector3(0f, localY, 0f);
if (Physics.CheckBox(randomSpawnPos, agentHalfExtents, rot) == false)
{
foundNewSpawnLocation = true;
Expand Down

0 comments on commit 8a2fcc2

Please sign in to comment.