发射导弹后绕圈飞行路径



// 带有抖动效果的。
using UnityEngine;
using System.Collections;

public class Fly : MonoBehaviour { // must be smooth in the procedure ?

    Vector3 point; // fly with bullet but just go along a straight line..
    //Transform empty; // fly with bullet but just go along a straight line..
    Vector3 dir; // the direction empty obj goes to .
    float speed;
    float speed1,speed2;
    float rotSpeed;
    float tmpF;
    bool B; // minus rotSpeed or plus it..
    int bDir; // which direction the bullet rotate to ..
    float x0,y0,z0,a,b,c,x,y,z;
    // Use this for initialization
    void Start () {
        if (Random.Range (0, 2) == 0)
                        bDir = 1;
                else
                        bDir = -1;
        B = true;
        rotSpeed = 600;
        speed = 1; // speed along dir..
        speed1 = 5f; // speed go away from point.
        //speed2 = 2; // speed go close to point.
        dir = transform.TransformDirection (Vector3.forward);
        //empty = GameObject.CreatePrimitive (PrimitiveType.Cube);
        point = transform.position;
        x0 = point.x; y0 = point.y; z0 = point.z;
        a = dir.x; b = dir.y; c = dir.z;
        // get a point on the plane vertical to dir and contain point..
        x = x0; y = y0 + 0.1f; z = -0.1f*b / c + z0;
        //transform.Translate (x,y,z,Space.World);
        transform.position = new Vector3 (x,y,z);
        transform.RotateAround (point,dir,Random.Range(0.1f,360f));
        StartCoroutine ("GoAway");
        StartCoroutine ("Des");
    }

    IEnumerator GoAway()
    {
        yield return new WaitForSeconds (0.7f);

        speed1 = 0;
        //StartCoroutine ("Smooth");
        // Transform phase..
        yield return new WaitForSeconds (0.3f);
        //speed1 = -3.5f;
        speed = 10;
        B = false;
    }

    //IEnumerator Smooth()
    //{

    //}

    IEnumerator Des()
    {
        yield return new WaitForSeconds (3);
        Destroy (gameObject);
    }
    
    // Update is called once per frame
    void Update () {
        //Debug.LogError (transform.position);
        tmpF = speed * Time.deltaTime;
        transform.Translate (transform.forward*tmpF);
        point += dir * tmpF;
        transform.Translate (speed1*(transform.position-point)*Time.deltaTime,Space.World);
        transform.RotateAround (point,dir,bDir*rotSpeed*Time.deltaTime);
                if (B)
                        rotSpeed -= 600 * Time.deltaTime;
                else {
                    rotSpeed += 300 * Time.deltaTime;    
                    speed1-=5*Time.deltaTime;
                //    speed+=5*Time.deltaTime;
            }
    }
}


柔和的效果:::



using UnityEngine;
using System.Collections;
                                    // let's try three phases..
public class Fly : MonoBehaviour { // must be smooth in the procedure ?

    Vector3 point// fly with bullet but just go along a straight line..
    //Transform empty; // fly with bullet but just go along a straight line..
    Vector3 dir// the direction empty obj goes to .
    float speed;
    float speed1,speed2;
    float rotSpeed;
    float t1,t2,t3// time1time2time3..
    float tmpF;
    bool phase1,phase2,phase3// minus rotSpeed or plus it..
    int bDir// which direction the bullet rotate to ..
    float x0,y0,z0,a,b,c,x,y,z
    // Use this for initialization
    void Start () {
        if (Random.Range (02) == 0)
                        bDir = 1;
                else
                        bDir = -1;
        //phase1 = true;
        rotSpeed = 300;

        speed = 5// speed along dir..
        speed1 = 2f// speed go away from point.



        t1 = 0.1f;  t2 = 0.4f;  //t3 = 0.3f;



        //speed2 = 2; // speed go close to point.
        dir = transform.TransformDirection (Vector3.forward);
        //empty = GameObject.CreatePrimitive (PrimitiveType.Cube);
        point = transform.position;
        x0 = point.xy0 = point.yz0 = point.z;
        a = dir.xb = dir.yc = dir.z;
        // get a point on the plane vertical to dir and contain point..
        x = x0y = y0 + 0.1f
        z = 0;
        if(Mathf.Abs (c)>Mathf.Epsilon)
        z = -0.1f*b / c + z0
        //transform.Translate (x,y,z,Space.World);
        transform.position = new Vector3 (x,y,z);
        transform.RotateAround (point,dir,Random.Range(0.1f,360f));
        StartCoroutine ("GoAway");
        StartCoroutine ("Des");
    }

    IEnumerator GoAway()
    {
        //yield return new WaitForSeconds (0.7f);
        phase1 = true;
        yield return new WaitForSeconds (t1);
        phase1 = false;
        phase2 = true;
        yield return new WaitForSeconds (t2);
        phase2 = false;
        // Transform phase..
        //yield return new WaitForSeconds (0.3f);
        //speed1 = -3.5f;
    }

    //IEnumerator Smooth()
    //{

    //}

    IEnumerator Des()
    {
        yield return new WaitForSeconds (3);
        Destroy (gameObject);
    }
    
    // Update is called once per frame
    void Update () {
        //Debug.LogError (transform.position);
        tmpF = speed * Time.deltaTime;
        transform.Translate (transform.forward*tmpF);
        point += dir * tmpF;
        transform.Translate (speed1*(transform.position-point)*Time.deltaTime,Space.World);
        transform.RotateAround (point,dir,bDir*rotSpeed*Time.deltaTime);
                
        if (phase1) {
                        rotSpeed -= 250 * Time.deltaTime;    
                        speed -= 1 * Time.deltaTime;
                        speed1-=1*Time.deltaTime;
                } else if (phase2) {
                        rotSpeed -= 50 * Time.deltaTime;    
                        speed += 1 * Time.deltaTime;    
                        speed1-=6*Time.deltaTime;
                } else {
            rotSpeed -= 100 * Time.deltaTime;    
            speed += 1 * Time.deltaTime;
            speed1-=1*Time.deltaTime;
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值