Cailean Finn
10 months ago
7 changed files with 501 additions and 26 deletions
@ -0,0 +1,8 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: c987654204484064897a7f1f98bf68e1 |
||||
|
folderAsset: yes |
||||
|
DefaultImporter: |
||||
|
externalObjects: {} |
||||
|
userData: |
||||
|
assetBundleName: |
||||
|
assetBundleVariant: |
@ -0,0 +1,166 @@ |
|||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using Unity.MLAgents; |
||||
|
using Unity.VisualScripting; |
||||
|
using UnityEngine; |
||||
|
using UnityEngine.Rendering; |
||||
|
|
||||
|
public class ControllerManager : MonoBehaviour |
||||
|
{ |
||||
|
public static ControllerManager Instance { get; private set; } |
||||
|
|
||||
|
public enum ControllerType { |
||||
|
Controller, |
||||
|
Waypoints |
||||
|
} |
||||
|
|
||||
|
public ControllerType m_CurrentControllerType; |
||||
|
|
||||
|
private GameObject m_WpManager; |
||||
|
|
||||
|
public Transform m_Target; |
||||
|
|
||||
|
public Transform m_ControllerTargetRoot; |
||||
|
|
||||
|
public Transform m_ControllerTarget; |
||||
|
|
||||
|
public Transform m_Chair; |
||||
|
|
||||
|
private GameObject m_Agent; |
||||
|
|
||||
|
public float m_Offset; |
||||
|
|
||||
|
private void Awake() |
||||
|
{ |
||||
|
if (Instance != null && Instance != this) |
||||
|
{ |
||||
|
Destroy(this); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Instance = this; |
||||
|
} |
||||
|
|
||||
|
SetControllerType(m_CurrentControllerType); |
||||
|
m_Agent = m_ControllerTargetRoot.transform.parent.gameObject; |
||||
|
} |
||||
|
|
||||
|
private void SetControllerType(ControllerType type) |
||||
|
{ |
||||
|
m_CurrentControllerType = type; |
||||
|
|
||||
|
m_WpManager = FindFirstObjectByType<WaypointManager>().gameObject; |
||||
|
|
||||
|
if (m_CurrentControllerType == ControllerType.Waypoints) |
||||
|
{ |
||||
|
m_WpManager.SetActive(true); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
m_WpManager.SetActive(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void Update() |
||||
|
{ |
||||
|
if (m_CurrentControllerType == ControllerType.Controller) |
||||
|
{ |
||||
|
if (m_Target != null) |
||||
|
{ |
||||
|
ProcessInput(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void ProcessInput() |
||||
|
{ |
||||
|
RotateControllerRoot(); |
||||
|
SetWalkingSpeed(); |
||||
|
CheckIfClimbing(); |
||||
|
SetTargetCubeTransform(); |
||||
|
} |
||||
|
|
||||
|
private void SetTargetCubeTransform() |
||||
|
{ |
||||
|
Vector3 chairPosition = new Vector3(m_Chair.position.x, m_Chair.position.y + m_Offset, m_Chair.position.z + m_Offset); |
||||
|
m_ControllerTargetRoot.transform.position = chairPosition; |
||||
|
|
||||
|
Vector3 controllerTargetPosition = m_ControllerTarget.transform.position; |
||||
|
m_Target.position = controllerTargetPosition; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private void RotateControllerRoot() |
||||
|
{ |
||||
|
var rotation = 0; |
||||
|
var rotationIncrement = 5; |
||||
|
|
||||
|
if(Input.GetKey(KeyCode.A)) |
||||
|
{ |
||||
|
rotation -= rotationIncrement; |
||||
|
} |
||||
|
|
||||
|
if(Input.GetKey(KeyCode.D)) |
||||
|
{ |
||||
|
rotation += rotationIncrement; |
||||
|
} |
||||
|
|
||||
|
m_ControllerTargetRoot.Rotate(new Vector3(0, rotation, 0)); |
||||
|
} |
||||
|
|
||||
|
private void SetWalkingSpeed() |
||||
|
{ |
||||
|
|
||||
|
string currentModel = m_Agent.GetComponent<ModelSwap>().m_currentModelName; |
||||
|
|
||||
|
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) |
||||
|
{ |
||||
|
m_Agent.GetComponent<Walker>().MTargetWalkingSpeed = 8f; |
||||
|
|
||||
|
} |
||||
|
else if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftShift)) |
||||
|
{ |
||||
|
m_Agent.GetComponent<Walker>().MTargetWalkingSpeed = 4f; |
||||
|
|
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
m_Agent.GetComponent<Walker>().MTargetWalkingSpeed = 0.1f; |
||||
|
} |
||||
|
|
||||
|
//Debug.Log($"{m_Agent.GetComponent<Walker>().MTargetWalkingSpeed}");
|
||||
|
} |
||||
|
|
||||
|
private void CheckIfClimbing() |
||||
|
{ |
||||
|
if (Input.GetKeyDown(KeyCode.Space)) |
||||
|
{ |
||||
|
if(m_Agent.GetComponent<ModelSwap>().m_currentModelName != "Stairs") |
||||
|
{ |
||||
|
// Swap to new model
|
||||
|
m_Agent.GetComponent<ModelSwap>().SwitchModel("Stairs", m_Agent.GetComponent<Agent>()); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
m_Agent.GetComponent<ModelSwap>().SwitchModel("Walker", m_Agent.GetComponent<Agent>()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (m_Agent.GetComponent<ModelSwap>().m_currentModelName == "Stairs") |
||||
|
{ |
||||
|
// Adjust height of controller target
|
||||
|
m_Offset = 2f; |
||||
|
m_Agent.GetComponent<Walker>().MTargetWalkingSpeed = 8f; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
m_Offset = 0; |
||||
|
} |
||||
|
|
||||
|
if (m_Agent.GetComponent<ModelSwap>().m_currentModelName == "Getup") |
||||
|
{ |
||||
|
m_Agent.GetComponent<Walker>().MTargetWalkingSpeed = 8f; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: 494dd9677a9308d48ab3ea7b149d6fce |
@ -1 +1 @@ |
|||||
{"count":1,"self":27.504924799999998,"total":27.816705199999998,"children":{"InitializeActuators":{"count":1,"self":0.0009989,"total":0.0009989,"children":null},"InitializeSensors":{"count":1,"self":0.0030057,"total":0.0030057,"children":null},"AgentSendState":{"count":1053,"self":0.016508699999999998,"total":0.046900399999999995,"children":{"CollectObservations":{"count":211,"self":0.0088525,"total":0.0088525,"children":null},"WriteActionMask":{"count":211,"self":0.0015241,"total":0.0015241,"children":null},"RequestDecision":{"count":211,"self":0.020015099999999997,"total":0.020015099999999997,"children":null}}},"DecideAction":{"count":1053,"self":0.2454862,"total":0.2496176,"children":{"RayPerceptionSensor.Perceive":{"count":211,"self":0.0041313999999999995,"total":0.0041313999999999995,"children":null}}},"AgentAct":{"count":1053,"self":0.0107549,"total":0.0107549,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1711459812","unity_version":"2023.2.8f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.2.8f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_03-Beep\\beep-final -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Beep","end_time_seconds":"1711459840"}} |
{"count":1,"self":160.07833599999998,"total":161.7581979,"children":{"InitializeActuators":{"count":1,"self":0.0016497999999999999,"total":0.0016497999999999999,"children":null},"InitializeSensors":{"count":1,"self":0.004039,"total":0.004039,"children":null},"AgentSendState":{"count":7589,"self":0.10136389999999999,"total":0.1957705,"children":{"CollectObservations":{"count":1518,"self":0.0520992,"total":0.0520992,"children":null},"WriteActionMask":{"count":1518,"self":0,"total":0,"children":null},"RequestDecision":{"count":1518,"self":0.042307399999999995,"total":0.042307399999999995,"children":null}}},"DecideAction":{"count":7589,"self":1.4234396999999999,"total":1.4318741,"children":{"RayPerceptionSensor.Perceive":{"count":1518,"self":0.0084344,"total":0.0084344,"children":null}}},"AgentAct":{"count":7589,"self":0.0455219,"total":0.0455219,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1711470994","unity_version":"2023.2.8f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.2.8f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_03-Beep\\beep-final -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Beep","end_time_seconds":"1711471155"}} |
Loading…
Reference in new issue