108 lines
3.0 KiB
C#
108 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CharacterPositionMovementController: Omni.OmniController
|
|
{
|
|
[Tooltip("Set to True if you want to use a joystick or WASD for testing instead of the Omni and no HMD. Please uncheck when you do a full build for release.")]
|
|
public bool developerMode = false;
|
|
|
|
private float joystickDeadzone = 0.05f;
|
|
private bool rotationCorrected = false;
|
|
|
|
[Header("----- Offset Variables -----")]
|
|
public int forwardRotationOffset = 0;
|
|
|
|
[Header("-----GameObject References-----")]
|
|
protected CharacterController characterController;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (!Application.isEditor)
|
|
{
|
|
developerMode = false;
|
|
}
|
|
|
|
characterController = GetComponent<CharacterController>();
|
|
Setup(developerMode);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (!developerMode)
|
|
{
|
|
UpdateForOmni();
|
|
}
|
|
|
|
if (!rotationCorrected)
|
|
{
|
|
if (omniSetupComponent.IsPresent())
|
|
CorrectSpawnForward();
|
|
}
|
|
|
|
UseInputToMovePlayer();
|
|
}
|
|
|
|
public virtual void DeveloperModeUpdate()
|
|
{
|
|
CheckInputForMovementTesting();
|
|
GetOmniInputForCharacterMovement(forwardRotationOffset, !developerMode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks joystick or WASD for input if in developer mode
|
|
/// </summary>
|
|
void CheckInputForMovementTesting()
|
|
{
|
|
//Get values from joystick or WASD.
|
|
float inputX = Input.GetAxis("Horizontal");
|
|
float inputY = Input.GetAxis("Vertical");
|
|
|
|
//Clamp to deadzone
|
|
if (inputY < joystickDeadzone && inputY > 0 || inputY > -joystickDeadzone && inputY < 0)
|
|
{
|
|
inputY = 0;
|
|
}
|
|
|
|
if (inputX < joystickDeadzone && inputX > 0 || inputX > -joystickDeadzone && inputX < 0)
|
|
{
|
|
inputX = 0;
|
|
}
|
|
|
|
omniDataComponent.hidInput.y = inputY;
|
|
omniDataComponent.hidInput.x = inputX;
|
|
}
|
|
|
|
void UseInputToMovePlayer()
|
|
{
|
|
if (omniConnectionComponent.omniFound)
|
|
GetOmniInputForCharacterMovement(forwardRotationOffset, !developerMode);
|
|
else if (developerMode)
|
|
DeveloperModeUpdate();
|
|
|
|
if (GetForwardMovement() != Vector3.zero)
|
|
characterController.Move(GetForwardMovement());
|
|
if (GetStrafeMovement() != Vector3.zero)
|
|
characterController.Move(GetStrafeMovement());
|
|
}
|
|
|
|
void CorrectSpawnForward()
|
|
{
|
|
rotationCorrected = true;
|
|
|
|
Vector3 resultRotation = new Vector3(0.0f, 0.0f, 0.0f);
|
|
Vector3 spawnRotation = transform.rotation.eulerAngles;
|
|
Vector3 cameraRotation = cameraReference.rotation.eulerAngles;
|
|
|
|
float spawnRotationYaw = spawnRotation.y;
|
|
float cameraYaw = cameraRotation.y;
|
|
float difference = spawnRotationYaw - cameraYaw;
|
|
|
|
resultRotation.y = difference;
|
|
|
|
transform.Rotate(resultRotation);
|
|
}
|
|
}
|