enum
enum

Reputation: 51

Unity3D Flying AI movement (Helicopter)

I want make an helicopter ai for game, but i can't figure out how can i do flying movement. I need 2 types of movement(Flying around and Flying to) Helicopter will spawn at corner of map and fly to some player - FlyingTo after Heli will arrive to a player it will start Flying around and shoot missile.

I tried use transform.Translate, transform.rotation, rigidbody.force and many other things to movement, but didn't find anything working.

Few of my tries:


Vector3 flyto = (nearest.transform.position + new Vector3(0, 100, 0));
Vector3 Kouzlo1 = new Vector3(base.transform.position.x, 0, base.transform.position.z);
Vector3 Kouzlo2 = new Vector3(nearest.transform.position.x, 0, nearest.transform.position.z);

/*Vector3 targetDir = flyto - transform.position;
float step = 10 * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
transform.rotation = Quaternion.LookRotation(newDir);*/


if (Vector3.Distance(Kouzlo1, Kouzlo2) < 100)
{
 var rotate = Quaternion.LookRotation(flyto - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, 
 Time.deltaTime * 10);
 transform.Translate(Vector3.forward * 10 * Time.deltaTime);

 /*transform.position = new Vector3(transform.position.x - 
 Vector3.Distance(Kouzlo1, Kouzlo2), nearest.transform.position.y + 100, 
 transform.position.z - Vector3.Distance(Kouzlo1, Kouzlo2));
 Vector3 vvv = transform.rotation * new Vector3(0f, 1f, 0f);

 transform.RotateAround(flyto, vvv, 180 * Time.deltaTime);*/



 //transform.RotateAround(flyto, Vector3.forward, 10 * Time.deltaTime);
}else {
 //transform.rotation = Quaternion.Slerp(transform.rotation, 
 Quaternion.LookRotation(flyto -transform.position), 10 * Time.deltaTime);

 //transform.position += transform.forward * 10 * Time.deltaTime;
 transform.LookAt(nearest.transform.position);

 rigidbody.AddForce(Vector3.forward * 10);
}


Upvotes: 0

Views: 970

Answers (1)

shingo
shingo

Reputation: 27086

You need to understand the method Lerp / Slerp first.

Lerp(a, b, t);

When t=0 it returns a, t=1 it returns b, otherwise it returns a value between a and b.

So you may use

Quaternion.Slerp(a, b, t += Time.deltaTime * 10);

And there is another similar method RotateTowards (or MoveTowards in Vector3 / Mathf class) can do same work

Quaternion.RotateTowards(a, b, Time.deltaTime);

Upvotes: 1

Related Questions