0

故事是我有一个研究项目,我必须在其中模拟一个跟踪实验。

我在一个盒子里有一个球(我们希望将来可以实现为 VR 房间),球移动到随机坐标。

我还计划添加另一个对象,用户可以单击(或者在 VR 的情况下,使用操纵杆)来移动和跟随球。

小球每次运动的坐标,物体每次运动的坐标都必须输出到文件中。

现在,我很难将球发送到随机坐标。

球名为“Player”,文件名为“PlayerController”。这两个问题是,在使用 Random 的 Unity 版本时,我在每次运行时始终获得相同的坐标,并且球不会停止移动。

我的代码附在下面。此外,如果你们中的任何一位读者对如何实施我的项目的其余部分有任何想法,我们非常感谢您提出建议!

太感谢了!

using System.Collections;
using System.Collections.Generic;
using System.IO;

using UnityEngine;

public class PlayerController : MonoBehaviour {
    private float movementDuration = 2.0f;
    private float waitBeforeMoving = 2.0f;
    private bool hasArrived = false;
    private Coroutine moveCoroutine = null;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (!hasArrived)
        {
            hasArrived = true;



            Vector3[] v = new Vector3[20];




            for (int i=0; i<v.Length; i++)
            {
                Random.InitState(System.DateTime.Now.Millisecond);

                v[i] = new Vector3(Random.Range(-10.0f, 10.0f),
                Random.Range(-10.0f, 10.0f),
               Random.Range(-10.0f, 10.0f));

                moveCoroutine = StartCoroutine(MoveToPoint(v[i]));
            }


        }
        if (Input.GetMouseButtonDown(0))
            StopMovement();
    }

    private IEnumerator MoveToPoint(Vector3 targetPos)
    {
        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, targetPos, t);

            yield return null;
        }

        yield return new WaitForSeconds(waitBeforeMoving);
        hasArrived = false;
    }

    private void StopMovement()
    {
        if (moveCoroutine != null)
            StopCoroutine(moveCoroutine);
    }

    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("sphereTag"))
            StopMovement();
    }
4

1 回答 1

0

球不会停止移动,因为您在协程结束时将 hasArrived 设置为 false,因此它每 2 秒再次触发一次。

每次坐标对我来说都是不同的 - 不确定你在那里面临什么问题。可能是 Random.InitState()

设置循环的方式是,数组中的每个向量都被输入到自己的协程中,但这一切基本上都是瞬时发生的,因为在进入 for 循环的下一次迭代之前没有延迟。如果您尝试遍历所有 20 个点,则应改为调用单个协程,其中包含 for 循环和嵌入式延迟:

bool hasArrived;
private float movementDuration = 2.0f;
private float waitBeforeMoving = 2.0f;

void Update()
{
    if (!hasArrived)
    {
        hasArrived = true;
        StartCoroutine(MoveToPoints());
    }
}

private IEnumerator MoveToPoints()
{
    Vector3[] v = new Vector3[20];

    for (int i = 0; i < v.Length; i++)
    {
        float timer = 0.0f;
        Vector3 startPos = transform.position;
        v[i] = new Vector3(Random.Range(-10.0f, 10.0f),
                Random.Range(-10.0f, 10.0f),
                Random.Range(-10.0f, 10.0f));

        while (timer < movementDuration)
        {
            timer += Time.deltaTime;
            float t = timer / movementDuration;
            t = t * t * t * (t * (6f * t - 15f) + 10f);
            transform.position = Vector3.Lerp(startPos, v[i], t);
            yield return null;
        }
        yield return new WaitForSeconds(waitBeforeMoving);
    }
}
于 2018-12-09T22:08:16.500 回答