jimmy tremblay-Bernier c1bf5a4ca1 initial commit
2022-03-12 22:04:30 -04:00

176 lines
6.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Omni
{
public class OmniController : MonoBehaviour
{
#region Movement Variable
[Header("-----Movement Options-----")]
[Tooltip("Affects overall speed forward, back, left, right.")]
public float maxSpeed = 10;
[Range(0.0f, 1.0f)]
[Tooltip("Multiplier to reduce strafing speed (Strafing at full speed can feel too fast).")]
public float strafeSpeedMultiplier = 1.0f;
[Range(0.0f, 1.0f)]
[Tooltip("Multiplier to reduce backwards speed (Moving backwards at full speed can feel too fast).")]
public float backwardsSpeedMultiplier = 1.0f;
[Tooltip("Multiplier for gravity for character controller.")]
public float gravityMultiplier = 1;
#endregion
[Header("-----GameObject References-----")]
public Transform cameraReference;
public Transform dummyObject;
protected Vector3 dummyForward;
private Vector3 forwardMovement;
private Vector3 strafeMovement;
protected OmniManager omniManager;
protected OmniSetupComponent omniSetupComponent;
protected OmniConnectionComponent omniConnectionComponent;
protected OmniDataComponent omniDataComponent;
// Start is called before the first frame update
protected void Setup(bool developerMode = false)
{
omniManager = new OmniManager();
omniSetupComponent = new OmniSetupComponent();
gameObject.AddComponent<OmniConnectionComponent>();
gameObject.AddComponent<OmniDataComponent>();
omniConnectionComponent = GetComponent<OmniConnectionComponent>();
omniDataComponent = GetComponent<OmniDataComponent>();
//cameraReference = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform;
omniDataComponent.Setup(omniManager);
omniSetupComponent.Setup(omniManager, omniConnectionComponent, omniDataComponent);
InitializeForOmni(developerMode);
omniConnectionComponent.Connect(omniDataComponent);
}
private bool IsCameraReferenceUnknown()
{
if (cameraReference == null)
{
Debug.LogError("Camera Reference not set in prefab.");
return true;
}
return false;
}
protected virtual void InitializeForOmni(bool developerMode = false)
{
if (developerMode)
{
if (IsCameraReferenceUnknown()) return;
if (!omniSetupComponent.IsPresent())
{
cameraReference.gameObject.AddComponent<SmoothMouseLook>();
Vector3 adjustedCameraPosition = cameraReference.localPosition;
adjustedCameraPosition.y = 1.5f;
cameraReference.localPosition = adjustedCameraPosition;
}
}
if (omniManager.FindOmni())
{
Debug.Log(System.DateTime.Now.ToLongTimeString() + ": OmniMovementComponent(OmniInitialize) - Successfully found the Omni.");
omniConnectionComponent.omniFound = true;
}
else
{
Debug.LogError(System.DateTime.Now.ToLongTimeString() + ": OmniMovementComponent(OmniInitialize) - Attempted to Initialize the Omni, but Omni not found.");
omniConnectionComponent.omniFound = false;
return;
}
}
public void GetOmniInputForCharacterMovement(int forwardRotationOffset = 0, bool needsCoupling = true)
{
if (IsCameraReferenceUnknown()) return;
Vector2 hidInput = omniDataComponent.hidInput;
forwardMovement = new Vector3(0.0f, 0.0f, 0.0f);
strafeMovement = forwardMovement;
//calculate angle between camera and omni
omniDataComponent.ComputeAngleBetweenControllerAndCamera(cameraReference);
float forwardRotation = omniDataComponent.CalculateForwardRotation(needsCoupling) + forwardRotationOffset;
//display forward rotation defined by our coupling percentage
dummyObject.rotation = Quaternion.Euler(0, forwardRotation, 0);
dummyForward = dummyObject.forward;
//calculate forward movement
Vector3 movementInputVector = new Vector3(hidInput.y * dummyForward.x, 0, hidInput.y * dummyForward.z);
//apply multiplier to reduce backwards movement speed by a given percentage
if (hidInput.y < 0) { movementInputVector *= backwardsSpeedMultiplier; }
//display rounded values
if (dummyForward.x != 0)
dummyForward.x = Mathf.Round(dummyForward.x * 100) / 100;
if (dummyForward.z != 0)
dummyForward.z = Mathf.Round(dummyForward.z * 100) / 100;
//apply gravity
movementInputVector += Physics.gravity * gravityMultiplier;
//perform forward movement
forwardMovement = (movementInputVector * maxSpeed * Time.deltaTime);
if (hidInput.x != 0)
{
//calculate strafe movement
movementInputVector = new Vector3(hidInput.x * dummyObject.right.x, 0, hidInput.x * dummyObject.right.z);
//apply modifier to reduce strafe movement by a given percentage
movementInputVector *= strafeSpeedMultiplier;
//apply gravity
movementInputVector += Physics.gravity * gravityMultiplier;
//perform strafe movement
strafeMovement = (movementInputVector * maxSpeed * Time.deltaTime);
}
}
public Vector3 GetForwardMovement()
{
return forwardMovement;
}
public Vector3 GetStrafeMovement()
{
return strafeMovement;
}
public void UpdateForOmni()
{
omniDataComponent.ReadAndSetOmniData(omniConnectionComponent, omniSetupComponent.IsInitialized());
omniSetupComponent.CalibrateOmni(cameraReference);
}
//called on exit
void OnApplicationQuit()
{
omniSetupComponent.StopOmni();
}
//called on scene change
void OnDisable()
{
omniSetupComponent.StopOmni();
omniConnectionComponent.StopReconnectAttempts();
}
}
}