怎样才能让船只在旋转后朝着它们面向的方向移动?

时间:2016-10-09 07:24:21

标签: c# unity3d

在剧本中我有20艘太空船。我存储每个船的起始位置,然后检查每艘船是否移动并且在我旋转船的过程中行驶了50个距离。这部分工作正常。

现在我想要做到这一点,如果我将每艘船旋转180度,一旦船旋转移动到这个方向。问题是即使在旋转之后,船仍继续向原始方向移动。

在剧本的顶部:

private float distanceTravelled;
public bool updateOn = true;
private Vector3 lastPosition;

List<bool> hasRotated = new List<bool>();
List<float> distanceTraveled = new List<float>();
List<Vector3> lastPositions = new List<Vector3>();

在“开始”功能中,我添加了所有孩子的起始位置,并将每个孩子设置为假:

private void Start()
    {
        UpdateSpheres ();
        spheres = GameObject.FindGameObjectsWithTag("MySphere");
        for(int index = 0; index < spheres[0].transform.childCount;index++)
        {
            Transform child = spheres[0].transform.GetChild(index);
            lastPosition = new Vector3(child.transform.position.x,child.transform.position.y,child.transform.position.z);
            lastPositions.Add (lastPosition);
            hasRotated.Add(false);
            distanceTraveled.Add(0f);
        }
    }

在更新功能中,我创建了属于UpdateSpheres的船只,然后使用MoveShips移动和漫游:

private void Update()
    {
        UpdateSpheres();
        MoveShips ();
    }

然后在MoveShips中,我试图改变每个宇宙飞船旋转时的移动方向:

这是原始的MoveShips功能代码:

private void MoveShips()
    {
        for (int index = 0;index < spheres[0].transform.childCount;index++)
        {
            Transform oneChild = spheres[0].transform.GetChild(index);
            lastPositions[index] = oneChild.transform.position;
            oneChild.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
        }

        if (updateOn == true) {
            for(int index =0;index < spheres[0].transform.childCount;index++)
            {
                Transform child = spheres[0].transform.GetChild(index);

                distanceTraveled[index] += Vector3.Distance (child.transform.position, lastPositions [index]);
                if (distanceTraveled[index] >= 50 && !hasRotated[index])
                {
                    child.transform.Rotate (new Vector3 (0f, 180f, 0f));
                    hasRotated[index] = true;
                }
            }
        }
    }

然后我更改了MoveShips功能并尝试使用以下行:

oneChild.transform.position -= oneChild.transform.forward * Time.deltaTime * moveSpeed;

或者

child.transform.position += child.transform.forward * Time.deltaTime * moveSpeed;

但是在所有情况下,船只都会继续移动原来的方向,然后根据旋转后的位置不改变运动。

如果我将它们旋转1度20度或180度,我希望它们在旋转后移动到它们现在面向的方向。即使我改变X轴Y轴或Z轴上的旋转,然后向上向右上移动,这取决于我旋转的轴。但我现在首先要做的是将它们旋转180度,这样它们会在旋转后改变方向并向这个方向移动。现在向后移动,但面向方向。

所以我试图使用子转换而不是Vector3.forward但到目前为止没有成功。

private void MoveShips()
    {
        for (int index = 0;index < spheres[0].transform.childCount;index++)
        {
            Transform oneChild = spheres[0].transform.GetChild(index);
            lastPositions[index] = oneChild.transform.position;
            //oneChild.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
            if (distanceTraveled [index] >= 50 && !hasRotated [index]) {
                oneChild.transform.position -= oneChild.transform.forward * Time.deltaTime * moveSpeed;
            } else 
            {
                oneChild.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
            }

        }

        if (updateOn == true) {
            for(int index =0;index < spheres[0].transform.childCount;index++)
            {
                Transform child = spheres[0].transform.GetChild(index);

                distanceTraveled[index] += Vector3.Distance (child.transform.position, lastPositions [index]);
                if (distanceTraveled[index] >= 50 && !hasRotated[index])
                {
                    child.transform.Rotate (new Vector3 (0f, 180f, 0f));
                    hasRotated[index] = true;
                    //child.transform.position += child.transform.forward * Time.deltaTime * moveSpeed;
                }
            }
        }
    }

更新

TurnShip让它们变得平滑,但它们会保持向原始方向移动而不会在转弯后改变方向。

在脚本顶部添加:

public float smooth = 1f;
private Vector3 targetAngles;

这就是我的MoveShips现在的样子:

private void MoveShips()
    {
        for (int index = 0;index < spheres[0].transform.childCount;index++)
        {
            Transform oneChild = spheres[0].transform.GetChild(index);
            lastPositions[index] = oneChild.transform.position;
            oneChild.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
        }

        if (updateOn == true) {
            for(int index =0;index < spheres[0].transform.childCount;index++)
            {
                Transform child = spheres[0].transform.GetChild(index);

                distanceTraveled[index] += Vector3.Distance (child.transform.position, lastPositions [index]);
                if (distanceTraveled[index] >= 50 && !hasRotated[index])
                {
                    targetAngles = child.transform.eulerAngles + 180f * Vector3.up;
                    StartCoroutine (TurnShip (child.transform, child.transform.eulerAngles, targetAngles, smooth));
                    hasRotated[index] = true;
                }
            }
        }
    }

TurnShip功能:

IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
    {
        float lerpSpeed = 0;

        while(lerpSpeed < 1)
        {
            ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
            lerpSpeed += Time.deltaTime * smooth;
            yield return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

你试过Translate吗?

// Example Forwards
child.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
// Example Backwards
child.transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
// you could use: left, right, up, down, forward, back

文档:https://docs.unity3d.com/ScriptReference/Transform.Translate.html