# AI moving forward without spawnpoints?

 0 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 41 ● 6 ● 6 ● 11 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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 15.1k ● 16 ● 44 ● 137 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 transform.Translate(0,0,speed/- 20*Time.deltaTime); put it in the enemyscript more ▼ answered Jun 02 '10 at 07:25 PM Marcus 41 ● 6 ● 6 ● 11 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x983
x453
x213

asked: Jun 02 '10 at 07:18 PM

Seen: 1256 times

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