- Home /
AI enemy vechicle problem
I want to know about script for control a tank. I can make it look at and follow player but I have some problem. the enemy tank don't see player it will move to straight and when it move to something such as a wall or bigstone it can't turn away from that object so the enemy tank will fools because it can't move to anywhere.
Answer by aldonaletto · Apr 30, 2012 at 05:10 AM
That's not an easy question - avoiding obstacles is a complex task. In this case, maybe you could use the following algorithm: each Update, cast a short ray forward; if nothing is hit, move the tank; if it hits something, enter a state where the tank will rotate a fixed angle (15 degrees, for instance) then return to idle state, where it will again verify obstacles ahead - something like this:
enum State {Idle, Chase, Attack, TurnLeft, TurnRight}; var state: State = State.Idle; var turnSpeed: float = 60; // 60 degrees per second var length: float = 8; // "see" 8 units ahead private var turnAngle: float = 0;
function Update(){ switch (state){ case State.Chase: // chase the player case State.Idle: if (!Physics.Raycast(transform.position, transform.forward, length)){ // move the tank forward if no obstacles ahead } else { // if some obstacle in range, enter turn right state: state = State.TurnRight; turnAngle = 15; } break; case State.TurnRight: // stay in turn mode until turnAngle ends: var dAngle = turnSpeed * Time.deltaTime; transform.Rotate(0, dAngle, 0); turnAngle -= dAngle; // when turnAngle ended, return to idle state: if (turnAngle
Answer by JamesArndt · Dec 22, 2012 at 07:42 PM
Latest tutorial upload for all of you Unity folks. This is a video that will help you get simple AI going in your racing or traffic games. The video includes a 1.4MB download of the entire Unity project seen in this tutorial (look in the video description).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Traffic Script 1 Answer
Standard Assets/Car Waypoint Based: How to check if vehicle got to the last waypoint? 0 Answers
AI car script to follow a track 0 Answers
Lock rotation of object 4 Answers