You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.1 KiB
167 lines
4.1 KiB
9 months ago
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|