-
-
Save lachlansleight/dcbc2a9a5d74da925ad47f950b81edb9 to your computer and use it in GitHub Desktop.
Completed Fly Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace TiltBrush.LachlanSleight | |
{ | |
public class FlyTool : BaseTool | |
{ | |
private GameObject _toolDirectionIndicator; | |
private bool m_LockToController; | |
private Transform m_BrushController; | |
[SerializeField] | |
[Range(0f, 2f)] | |
private float m_MaxSpeed = 1f; | |
[SerializeField] | |
[Range(0f, 18f)] | |
private float m_DampingUp = 2f; | |
[SerializeField] | |
[Range(0f, 18f)] | |
private float m_DampingDown = 12f; | |
[SerializeField] | |
[Range(0f, 1f)] | |
private float m_StopThresholdSpeed = 0.01f; | |
private bool m_Armed = false; | |
private Vector3 m_Velocity; | |
public override void Init() | |
{ | |
base.Init(); | |
_toolDirectionIndicator = transform.Find("DirectionIndicator").gameObject; | |
} | |
override public void EnableTool(bool bEnable) | |
{ | |
base.EnableTool(bEnable); | |
if (bEnable) { | |
m_LockToController = m_SketchSurface.IsInFreePaintMode(); | |
if (m_LockToController) { | |
m_BrushController = InputManager.m_Instance.GetController(InputManager.ControllerName.Brush); | |
} | |
EatInput(); | |
} | |
m_Armed = false; | |
// Make sure our UI reticle isn't active. | |
SketchControlsScript.m_Instance.ForceShowUIReticle(false); | |
} | |
float BoundsRadius { | |
get { | |
return SceneSettings.m_Instance.HardBoundsRadiusMeters_SS; | |
} | |
} | |
public override bool InputBlocked() | |
{ | |
return !m_Armed; | |
} | |
public override void HideTool(bool bHide) | |
{ | |
base.HideTool(bHide); | |
_toolDirectionIndicator.SetActive(!bHide); | |
} | |
override public void UpdateTool() { | |
base.UpdateTool(); | |
//don't start teleporting until the user has released the trigger they pulled to enable the tool! | |
if (InputBlocked()) { | |
if (!InputManager.m_Instance.GetCommand(InputManager.SketchCommands.Fly)) { | |
m_Armed = true; | |
} else { | |
return; | |
} | |
} | |
Transform rAttachPoint = InputManager.m_Instance.GetBrushControllerAttachPoint(); | |
PointerManager.m_Instance.SetMainPointerPosition(rAttachPoint.position); | |
//keep the tool angle correct | |
m_toolDirectionIndicator.transform.localRotation = | |
Quaternion.Euler(PointerManager.m_Instance.FreePaintPointerAngle, 0f, 0f); | |
if (InputManager.m_Instance.GetCommand(InputManager.SketchCommands.Fly)) { | |
m_Velocity = Vector3.Lerp(m_Velocity, m_toolDirectionIndicator.forward * m_MaxSpeed, Time.deltaTime * m_DampingUp); | |
//Play world grab loop | |
AudioManager.m_Instance.WorldGrabLoop(true); | |
AudioManager.m_Instance.WorldGrabbed(m_BrushController.position); | |
AudioManager.m_Instance.ChangeLoopVolume("WorldGrab", | |
Mathf.Clamp((m_Velocity.magnitude / m_MaxSpeed) / | |
AudioManager.m_Instance.m_WorldGrabLoopAttenuation, 0f, | |
AudioManager.m_Instance.m_WorldGrabLoopMaxVolume)); | |
} else { | |
m_Velocity = Vector3.Lerp(m_Velocity, Vector3.zero, Time.deltaTime * m_DampingDown); | |
float mag = m_Velocity.magnitude; | |
if (mag < m_StopThresholdSpeed) { | |
//Stop the world grab audio loop | |
AudioManager.m_Instance.WorldGrabLoop(false); | |
m_Velocity = Vector3.zero; | |
} else { | |
//Fade world grab audio loop with speed | |
AudioManager.m_Instance.ChangeLoopVolume("WorldGrab", | |
Mathf.Clamp((mag / m_MaxSpeed) / | |
AudioManager.m_Instance.m_WorldGrabLoopAttenuation, 0f, | |
AudioManager.m_Instance.m_WorldGrabLoopMaxVolume)); | |
} | |
} | |
} | |
void ApplyVelocity(Vector3 velocity) | |
{ | |
TrTransform newScene = App.Scene.Pose; | |
newScene.translation -= velocity; | |
// newScene might have gotten just a little bit invalid. | |
// Enforce the invariant that fly always sends you | |
// to a scene which is MakeValidPose(scene) | |
newScene = SketchControlsScript.MakeValidScenePose(newScene, BoundsRadius); | |
App.Scene.Pose = newScene; | |
} | |
void Update() | |
{ | |
ApplyVelocity(m_Velocity); | |
if (!m_LockToController) { | |
// If we're not locking to a controller, update our transforms now, instead of in LateUpdate. | |
UpdateTransformsFromControllers(); | |
} | |
} | |
override public void LateUpdateTool() { | |
base.LateUpdateTool(); | |
UpdateTransformsFromControllers(); | |
} | |
private void UpdateTransformsFromControllers() { | |
// Lock tool to camera controller. | |
if (m_LockToController) { | |
transform.position = m_BrushController.position; | |
transform.rotation = m_BrushController.rotation; | |
} else { | |
transform.position = SketchSurfacePanel.m_Instance.transform.position; | |
transform.rotation = SketchSurfacePanel.m_Instance.transform.rotation; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment