x


AI moving forward without spawnpoints?

Hi i would like to know if its possible to get the ai to move forward without using waypoints im making 2d shoot em up flying game?

more ▼

asked Jun 02 '10 at 07:18 PM

Marcus gravatar image

Marcus
41 6 6 11

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I see you answered your own question, but I wanted to elaborate.

Yes, there are a number of ways depending on how complicated you want to make it. At the simplest level you could do:

function Update () {
     transform.Translate(Vector3.forward);
}

if you wanted something more complicated you could do something like:

private var movingAtAngle = false;
private var rotating = false;
private var spottedPos : Vector3;

var turnAngle = 30;
var sight = 10;
var speed = 20;

var returnToForwardPath = 5;

function Update () {
     if(!rotating)
          transform.Translate(Vector3.forward * speed * Time.deltaTime);
     if(Physics.Raycast(transform.position, transform.forward, sight) && !movingAtAngle) {
         rotating = true; 
         spottedPos = transform.position;
     }

     if(rotating) {
        transform.Rotate(0, turnAngle * Time.deltaTime, 0);
        if(Mathf.Abs(transform.eulerAngles.y) > turnAngle) {
            rotating = false;

            turnAngle =-turnAngle;
            movingAtAngle = true;
        }
     }

     if(movingAtAngle) {
          if(Vector3.Distance(transform.position, spottedPos) > returnToForwardPath) {
              transform.Rotate(0, turnAngle * Time.deltaTime, 0);
          }
          if(Mathf.Abs(transform.eulerAngles.y) < 3) {
              movingAtAngle = false;
          }
     }
}

This would have the enemy swerve to avoid the oncoming obstacle. It swerves predictably right the straightens out. It is very basic, it won't turn while not facing the global forward so it is a predictable AI pattern for shoot 'em up kind of games.

more ▼

answered Jun 02 '10 at 07:44 PM

Peter G gravatar image

Peter G
15k 16 44 136

(comments are locked)
10|3000 characters needed characters left

transform.Translate(0,0,speed/- 20*Time.deltaTime);

put it in the enemyscript

more ▼

answered Jun 02 '10 at 07:25 PM

Marcus gravatar image

Marcus
41 6 6 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x960
x441
x208

asked: Jun 02 '10 at 07:18 PM

Seen: 1233 times

Last Updated: Jun 02 '10 at 07:18 PM