为什么游戏对象在进入另一个游戏对象区域时没有完成旋转?

时间:2017-06-28 19:34:01

标签: c# unity3d unity5

这是我之前的问题:Previous question

答案是正确的,有效的。 一旦游戏对象开始进入另一个游戏对象区域,它就会做一些事情。

这是我附加到移动游戏对象的脚本,用于检查是否发生了碰撞:

using UnityEngine;

public class CheckBaseCollider : MonoBehaviour
{
    public GameObject baseCollider;
    public GameObject objectToRotate;
    public bool rotate = false;

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == baseCollider)
        {
            Debug.Log("Entered");

            rotate = true;
        }
    }

    private void Update()
    {
        if (rotate == true)
        {
            var str = Mathf.Min(.5f * Time.deltaTime, 1);
            objectToRotate.transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, str);
        }
    }
}

我有两个游戏对象。一个是Base(一个Cube,它的变量是baseTarget)。 “基地”在地形上。 我希望当移动的另一个GameObject开始进入“Base”时,它也将开始旋转。但由于某种原因,旋转开始然后停止,然后在几秒钟后恢复并完成。不知道为什么会停止。

这是移动游戏对象的脚本也附加到移动的游戏对象:

using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour
{

    public float maxVel = 30; // cruise speed
    public float startVel = 5; // speed at the starting point
    public float stopVel = 0.5f; // speed at the destination
    public float accDistance = 20; // acceleration/deceleration distance
    public float factor = 0.25f; // max inclination
    public float turnSpeed = 0.8f; // speed to turn/bank in the target direction
    Vector3 lastPos; // used to calculate current velocity
    Transform baseTarget;
    private Rigidbody _rigidbody;

    void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
        baseTarget = GameObject.Find("Base").transform;
        lastPos = transform.position;
        StartCoroutine(Fly()); // demo routine
    }
    // calculate bank/turn rotation at Update
    void Update()
    {
        // calculate the displacement since last frame:
        Vector3 dir = transform.position - lastPos;
        lastPos = transform.position; // update lastPos
        float dist = dir.magnitude;
        if (dist > 0.001f)
        { // if moved at least 0.001...
            dir /= dist; // normalize dir...
            float vel = dist / Time.deltaTime; // and calculate current velocity
                                               // bank in the direction of movement according to velocity
            Quaternion bankRot = Quaternion.LookRotation(dir + factor * Vector3.down * vel / maxVel);

            Vector3 rotation = Quaternion.Lerp(transform.rotation, bankRot, turnSpeed * Time.deltaTime).eulerAngles;
            rotation.x = 0;
            transform.rotation = Quaternion.Euler(rotation);
        }           
    }

    bool flying = false; // shows when FlyTo is running
                         // coroutine that moves to the specified point:
    IEnumerator FlyTo(Vector3 targetPos)
    {
        flying = true; // flying is true while moving to the target
        Vector3 startPos = transform.position;
        targetPos.y = startPos.y;
        Vector3 dir = targetPos - startPos;
        float distTotal = dir.magnitude;
        dir /= distTotal; // normalize vector dir
                          // calculate accDist even for short distances
        float accDist = Mathf.Min(accDistance, distTotal / 2);
        do
        {
            float dist1 = Vector3.Distance(transform.position, startPos);
            float dist2 = distTotal - dist1;
            float speed = maxVel; // assume cruise speed
            if (dist1 < accDist)
            { // but if in acceleration range...
              // accelerate from startVel to maxVel
                speed = Mathf.Lerp(startVel, maxVel, dist1 / accDist);
            }
            else
            if (dist2 < accDist)
            { // or in deceleration range...
              // fall from maxVel to stopVel
                speed = Mathf.Lerp(stopVel, maxVel, dist2 / accDist);
            }
            // move according to current speed:

            Vector3 pos = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
            //pos.y = lastPos.y;
            transform.position = pos;
            yield return 0; // let Unity breathe till next frame
        } while (transform.position != targetPos); // finish when target reached
        flying = false; // shows that flight has finished
    }
    // example routine: fly to 3 different points in sequence
    IEnumerator Fly()
    {
        Vector3 p0 = baseTarget.position;

        while (true)
        {
            StartCoroutine(FlyTo(p0));
            while (flying)
            {
                yield return 0;
            }
            yield return new WaitForSeconds(5);
        }
    }
}

0 个答案:

没有答案