Why after the spaceships collide with the box collider they are not turning back?

前端 未结 1 1265
广开言路
广开言路 2021-01-27 18:03

I want that when there is a collider turn the spaceship/s back. But they keep moving forward and out of the box collider and terrain.

The script that make the clones shi

相关标签:
1条回答
  • 2021-01-27 18:45

    It's because your Lerp method is only called once in OnTriggerExit. Lerp is usually used over time e.g. in Update or a coroutine. Here's how to do it in a coroutine:

    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Sphere") 
        {
            targetAngles = other.transform.eulerAngles + 180f * Vector3.up;
            StartCoroutine(TurnShip(other.transform, other.transform.eulerAngles, targetAngles, smooth))
        }
    }
    
    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题