// Start is called before the first frame update void Start() { this.meshRenderer = GetComponent <MeshRenderer>(); if (this.meshRenderer == null) { print("Failed to get this.meshRenderer"); return; } this.actsAsManipulatable = GetComponent <ActsAsManipulatable>(); if (this.actsAsManipulatable == null) { print("Failed to get this.actsAsManipulatable"); return; } ActsAsSwipeable actsAsSwipeable = GetComponent <ActsAsSwipeable>(); if (actsAsSwipeable != null) { actsAsSwipeable.swipeLeftDelegate = OnSwipeLeft; actsAsSwipeable.swipeRightDelegate = OnSwipeRight; } if (!MLPersistentCoordinateFrames.IsStarted) { MLResult resultStart = MLPersistentCoordinateFrames.Start(); switch (resultStart.Result) { case MLResult.Code.Ok: { commonStart(); } break; case MLResult.Code.PrivilegeDenied: throw new System.NotImplementedException(); case MLResult.Code.InvalidParam: throw new System.NotImplementedException(); case MLResult.Code.UnspecifiedFailure: throw new System.NotImplementedException(); } } else { commonStart(); } }
private void MLInput_OnTriggerUp(byte controllerId, float triggerValue) { if (triggerValue > 0.2) { return; } if (this.pickedGameObject == null && isTriggerDown == false) { return; } ActsAsManipulatable actsAsManipulatable = this.pickedGameObject.GetComponent<ActsAsManipulatable>(); if (actsAsManipulatable != null) { actsAsManipulatable.isBeingMoved = false; } isTriggerDown = false; this.pickedGameObject = null; }
// Update is called once per frame void Update() { if (MLInput.IsStarted) { /// /// Move /// if (isTriggerDown) { Vector3 cursorPosition = transform.position + (transform.forward * distanceWhenPicked); cursor.transform.position = cursorPosition; this.pickedGameObject.transform.position = cursorPosition; ActsAsManipulatable actsAsManipulatable = this.pickedGameObject.GetComponent<ActsAsManipulatable>(); if (actsAsManipulatable != null) { actsAsManipulatable.isBeingMoved = true; } return; } /// /// Hover /// Vector3 origin = transform.position; Vector3 direction = transform.forward; Ray ray = new Ray(origin, direction); if (Physics.Raycast(ray, out RaycastHit hitInfo, maxDistance, layerMask)) { Debug.DrawRay(ray.origin, ray.direction, Color.red); Debug.Assert(hitInfo.rigidbody != null, "rigidbody should not be null"); this.hoveredGameObject = hitInfo.rigidbody.gameObject; cursor.transform.position = this.hoveredGameObject.transform.position; /// /// Manipulatable and Hoverable /// ActsAsManipulatable actsAsManiputable = this.hoveredGameObject.GetComponent<ActsAsManipulatable>(); if (actsAsManiputable == null || !actsAsManiputable.isHoverable) { return; } /// /// Be sure to un-hover the previously hovered Game Object because they may be /// parallel, in line withthe User /// if (this.actsAsCursorHoverable) { this.actsAsCursorHoverable.isCursorHovering = false; } this.actsAsCursorHoverable = hoveredGameObject.GetComponent<ActsAsCursorHoverable>(); this.actsAsCursorHoverable.isCursorHovering = true; } else { // Let the MLRaycast.Raycast attempt to collide with the mesh // in the meantime, re-position the cursor and the // ActsAsRay script will extend the Line of the LineRenderer cursor.transform.position = mlRaycastHitPoint; cursor.transform.LookAt(mlRaycastHitPoint + mlRaycastHitNormal); cursor.transform.localScale = Vector3.one; if (this.actsAsCursorHoverable == null) { return; } this.actsAsCursorHoverable.isCursorHovering = false; this.hoveredGameObject = null; this.actsAsCursorHoverable = null; } }
private void MLInput_OnTriggerDown(byte controllerId, float triggerValue) { if (triggerValue < 0.8) { return; } if (this.hoveredGameObject == null) { return; } /// /// Manipulatable /// ActsAsManipulatable actsAsManiputable = this.hoveredGameObject.GetComponent<ActsAsManipulatable>(); if (actsAsManiputable == null) { return; } /// /// Triggerdown clones immediately /// if (actsAsManiputable.isClonable) { isTriggerDown = true; /// /// Clone the hovered Game Object, turn off hovering for the hovered game object /// this.pickedGameObject = Object.Instantiate(this.hoveredGameObject); this.actsAsCursorHoverable = hoveredGameObject.GetComponent<ActsAsCursorHoverable>(); this.actsAsCursorHoverable.isCursorHovering = false; /// /// The picked Game Object is now the hovering game object /// this.hoveredGameObject = this.pickedGameObject; ActsAsManipulatable pickedActsAsManiputable = this.pickedGameObject.GetComponent<ActsAsManipulatable>(); pickedActsAsManiputable.isClonable = false; pickedActsAsManiputable.isMovable = true; distanceWhenPicked = Vector3.Distance(transform.position, pickedGameObject.transform.position); return; } /// /// if not Clonable, then Triggerdown will pick /// the hovered object for moving. /// if (actsAsManiputable.isMovable) { if (this.hoveredGameObject == this.pickedGameObject && isTriggerDown == true) { return; } isTriggerDown = true; this.pickedGameObject = this.hoveredGameObject; distanceWhenPicked = Vector3.Distance(transform.position, pickedGameObject.transform.position); } /// /// If triggerable, then Triggerdown /// if (actsAsManiputable.isTriggerable) { actsAsManiputable.OnTrigger(controllerId, triggerValue); } }