空间中的玩家和用户立方体:
我想为我目前正在从事的项目寻求想法和帮助。
在此项目中,用户利用鼠标移动立方体(称为用户)以跟踪球体(称为播放器)的随机运动。有20个随机动作。当用户移动多维数据集时,它只能在x-y平面或x-z平面上移动,并且通过使用Shift键可以切换模式。然后,每3秒显示20个球体运动坐标和立方体(用户)坐标。
尽管具有此功能,但该程序无法正常运行。操纵立方体非常困难。有时,换档压力机无法很好地套准,因此很难在x-y平面上移动到x-z平面上。这不是最佳的用户体验。如果多维数据集不可见,则几乎不可能将其取回。另外,我很难弄清楚如何在盒子环境中绑定立方体和球体。
我希望我将来可以在Windows机器上针对Oculus Rift进行调整(我在MacBook Pro上运行),但是截至目前,我只是在尝试完成此原型合理的用户功能。
任何建议或帮助将不胜感激。
下面包含的是我GitHub上整个项目的链接,以及我项目的主要代码。
谢谢你,节日快乐!
链接:https://github.com/debarshikundu/TrackingExperiments
PlayerController类(控制领域):
using System.Collections;
using System.IO;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
float movementDuration = 2.0f;
WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
Vector3[] path = new Vector3[20];
void Start()
{
StartCoroutine(MainRoutine());
}
IEnumerator MainRoutine()
{
//generate new path:
for (int i = 0; i < path.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
float x = RandomNum(timer);
float y = RandomNum(x);
float z = RandomNum(y);
path[i] = new Vector3(x, y, z);
}
//traverse path:
for (int i = 0; i < path.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
while (timer < movementDuration)
{
timer += Time.deltaTime;
float t = timer / movementDuration;
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp(startPos, path[i], t);
yield return null;
}
yield return waitBeforeMoving;
}
//print path:
PrintPoints();
}
void PrintPoints()
{
string filepath = Application.dataPath + "/Player.txt";
if (File.Exists(filepath) == false)
{
File.WriteAllText(filepath, "The player blob visited these random coordinates: \n\n");
}
foreach (Vector3 vector in path)
{
File.AppendAllText(filepath, string.Format("{0}\n\n", JsonUtility.ToJson(vector)));
}
}
float RandomNum(float lastRandNum)
{
//Random value range can be changed in the future if necessary
float randNum = Random.Range(-10.0f, 10.0f);
return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
}
}
UserController类(使用户能够移动多维数据集):
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
[RequireComponent(typeof(MeshCollider))]
public class UserController : MonoBehaviour {
bool shiftOn = false;
void OnMouseDrag()
{
if (Input.GetMouseButton(0))
{
if(shiftOn)
{
//3D Drag, courtesy of Unity Forums
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
}
else
{
//Plane Drag
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
}
}
}
void changeShift()
{
shiftOn = !shiftOn;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
OnMouseDrag();
if (Input.GetKey(KeyCode.LeftShift)||Input.GetKey(KeyCode.RightShift))
{
changeShift();
}
}
}
CubeTracker类(每3秒跟踪一次多维数据集的坐标并输出到文件):
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class CubeTracker : MonoBehaviour {
private bool logging = true;
void Awake()
{
StartCoroutine(LogPosition());
}
private IEnumerator LogPosition()
{
while (logging)
{
PrintPoint(transform.position);
yield return new WaitForSeconds(3f);
}
}
void PrintPoint(Vector3 position)
{
string filepath = Application.dataPath + "/User.txt";
if (File.Exists(filepath) == false)
{
File.WriteAllText(filepath, "The player blob visited these random coordinates: \n\n");
}
File.AppendAllText(filepath, string.Format("{0}\n\n", JsonUtility.ToJson(position)));
}
}