// 带有抖动效果的。
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; // time1, time2, time3..
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 (0, 2) == 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.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;
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;
}
}
}