- Home /
Need help moving a book gameobject to a preview position. Using VRTK
So Im making a VR scene where the player will be able to hover the pointer over books. As they hover over a book it will come forward to the preview position where they will be able to see the front of the book where the title is. When the book is in this preview position it will wait 3 seconds then return to its origin position. If in the 3 second wait time the player clicks the trigger on the book. It will move forward again to the reading position.
The complexity arises when there is more than one book. as the player hovers over a book then moves to the next one the previous book must cancel its move and return to its origin position.
I have been working on this script for 2 days now. The first attempt I made was by muting the move in the fixed update method and using a bunch ob bools to control when the book should move. But this quickly became too complex so I scrapped the script after 4 hours of working on it and started again.
My current approach is to use an IEnumerator and call this. I am still having issues. I'm only concentrating on the fist part of the feature (moving the books to the preview position) for now. I need the player to be able to point the cursor at any book and it pops out and any of the other ones currently in the middle of a movement return to their destination. So if the player moves the pointer horizontally across a row of books each one pops out and the previous one returns.
I would like to know what you guys think the best approach is. There is still alot of things in C# that I don't understand so my worry is that there is a better approach to coding this than the way i'm doing now.
Here is my current code. (Still not working )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class BookViewer : MonoBehaviour {
public Transform previewPos;
public Transform viewPos;
public float speed = 10;
[Header("VRTK")]
public VRTK_Pointer pointer;
public VRTK_InteractableObject linkedObject;
[Header("Bools")]
public bool bookLocked;
public bool previewingBook;
public bool moveComplete;
BookViewer[] activeBooks;
Vector3 originalPos;
Quaternion originalRot;
protected virtual void OnEnable()
{
//----------Set Up Positions-------------------------------------------------------------------
originalPos = gameObject.transform.position;
originalRot = gameObject.transform.rotation;
//----------Referances-------------------------------------------------------------------------
activeBooks = FindObjectsOfType<BookViewer>();
linkedObject = (linkedObject == null ? GetComponent<VRTK_InteractableObject>() : linkedObject);
//----------Subscribe to events----------------------------------------------------------------
if (linkedObject != null)
{
linkedObject.InteractableObjectUsed += InteractableObjectUsed;
linkedObject.InteractableObjectTouched += InteractableObjectTouched;
linkedObject.InteractableObjectUnused += InteractableObjectUnused;
linkedObject.InteractableObjectUntouched += InteractableObjectUnouched;
}
//---------------------------------------------------------------------------------------------
}
protected virtual void OnDisable()
{
//-------Unsubscribe From Events--------------------------------------------
if (linkedObject != null)
{
linkedObject.InteractableObjectUsed -= InteractableObjectUsed;
linkedObject.InteractableObjectTouched -= InteractableObjectTouched;
linkedObject.InteractableObjectUnused -= InteractableObjectUnused;
linkedObject.InteractableObjectUntouched -= InteractableObjectUnouched;
}
//--------------------------------------------------------------------------
}
/// <summary>
/// When the user hovers over a book it moves it to the preview position.
/// ANy other books in the preview posision or in the middle of a move will return to origin.
/// </summary>
void InteractableObjectTouched(object sender, InteractableObjectEventArgs e)
{
MoveBook(previewPos.position, previewPos.rotation, speed, 0);
}
/// <summary>
/// When the user unhovers from a book the book will continue its move to the preview position.
/// Unless the corsor moves onto another book in which case the previous book will return to origin.
/// If the book makes it to the prevew position it will wait there for 3 seconds then return to origin.
/// </summary>
void InteractableObjectUnouched(object sender, InteractableObjectEventArgs e)
{
MoveBook(originalPos, originalRot, speed, 3);
}
/// <summary>
/// If the book is in the previw position it will be interactable so the player can click on it.
/// Player will only be able to click if the book is in prevew position.
/// Book will move to the reading position when clicked.
/// When book is in reading position the hovering over other books will be disabled.
/// </summary>
protected virtual void InteractableObjectUsed(object sender, InteractableObjectEventArgs e)
{
}
/// <summary>
/// When book is in reading position and it is clicked again it will return to its origin position.
/// Other books will then be unlocked and be able to be hoverd over and previewd again.
/// </summary>
protected virtual void InteractableObjectUnused(object sender, InteractableObjectEventArgs e)
{
}
private void Update()
{
print (moveComplete);
}
public void ReturnBook(int delay)
{
StartCoroutine(MoveObject(originalPos, originalRot, speed, delay));
}
public void MoveBook(Vector3 _targetPos, Quaternion _targetRot, float _speed, float delay)
{
if (bookLocked) return;
StartCoroutine(MoveObject(_targetPos, _targetRot, _speed, delay));
foreach(BookViewer book in activeBooks)
{
if (book.gameObject != gameObject) book.ReturnBook(0);
}
}
public IEnumerator MoveObject(Vector3 _targetPos, Quaternion _targetRot, float _speed, float delayTime)
{
yield return new WaitForSeconds(delayTime);
while (gameObject.transform.position != _targetPos || gameObject.transform.rotation != _targetRot)
{
float step = _speed * Time.deltaTime;
moveComplete = false;
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, _targetPos, step);
gameObject.transform.rotation = Quaternion.RotateTowards(gameObject.transform.rotation, _targetRot, step * 100);
yield return new WaitForFixedUpdate();
//print("TargetPos_" + targetPos+"\nCurrentPos_"+gameObject.transform.position);
}
moveComplete = true;
}
}
Here is a screen shot of the books in unity.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D Character Control Help 0 Answers