- Home /
Please help me with AI
Im creating a 3rd person sci-fi tank game an I need an AI script without animation as the tanks float.
Raycast or waypoint would be alright (never used these methods)
thanks in advanced.
Answer by Cool Dave · Aug 22, 2012 at 04:32 PM
Here's my code from a car AI I made. With a little adjustment for tank steering, it should work fine. Actually, you could simplify it a lot for a tank. Hope it works!
`//var target : Transform;
var waypoints : Transform[];
var frontLeftColl : Collider;
var frontRightColl : Collider;
var rearLeftColl : Collider;
var rearRightColl : Collider;
var armLeft : Transform;
var armRight : Transform;
var frontLeft : Transform;
var frontRight : Transform;
var rearLeft : Transform;
var rearRight : Transform;
var carCenter : Transform;
var steering = 0.00;
var graphicSteering = 0.00;
var angleDisplay = 0.00;
var engineTorque = 0.00;
var carAngle = 0.00;
var maxSpeed = 0.00;
var xDistance = 0.00;
var zDistance = 0.00;
var loop : boolean = false;
var targetWaypoint : int;
var waypointRadius = 0.00;
var angularDistance = 0.00;
function FixedUpdate () {
wheelRpm = frontLeftColl.rpm;
//Waypoint System target = waypoints[targetWaypoint];
xDistance = target.position.x - carCenter.position.x; zDistance = target.position.z - carCenter.position.z;
angularDistance = (Mathf.Sqrt(xDistance xDistance) + (zDistance zDistance));
if (angularDistance < waypointRadius){ targetWaypoint += 1; } if (loop == true){ if(targetWaypoint >= waypoints.Length){ targetWaypoint = 0; } } //targetwaypoint is a number.
//var targetDir = target.position - frontLeft.position;
//var forward = frontLeft.forward;
//var angle = Vector3.Angle(targetDir, forward);
/* carCenter.LookAt(target);
armRight.rotation = Quaternion.Slerp (armLeft.rotation, carCenter.rotation, Time.time 1); armLeft.rotation = Quaternion.Slerp (armLeft.rotation, carCenter.rotation, Time.time 1); frontLeftColl.steerAngle = carCenter.rotation.y; frontRightColl.steerAngle = carCenter.rotation.y; //steering = Quaternion.EulerRotation(target); //steering = blahBlah.rotation.y; //armRight.LookAt(steering);
*/
var angle : float = Mathf.Atan2(xDistance, zDistance) * Mathf.Rad2Deg;
carAngle = carCenter.eulerAngles.y; steering = (angle - carAngle); angleDisplay = angle;
frontLeftColl.steerAngle = steering; frontRightColl.steerAngle = steering;
graphicSteering = steering;
if (graphicSteering > 360){ graphicSteering -= 360; } if (graphicSteering < 0){ graphicSteering += 360; }
if (graphicSteering > 45){ if (graphicSteering < 315){ graphicSteering = 45; }} if (graphicSteering < 315){ if (graphicSteering > 45){ graphicSteering = 315; }}
armLeft.localEulerAngles.y = (graphicSteering); armRight.localEulerAngles.y = (graphicSteering);
if (frontLeftColl.rpm > maxSpeed){ finalTorque = 0; } if (frontLeftColl.rpm < maxSpeed){ finalTorque = engineTorque; }
frontLeftColl.motorTorque = finalTorque; frontRightColl.motorTorque = finalTorque;
frontLeft.Rotate(Vector3.right wheelRpm ( 360/60 ) Time.deltaTime); frontRight.Rotate(Vector3.right wheelRpm ( 360/60 ) Time.deltaTime); rearLeft.Rotate(Vector3.right wheelRpm ( 360/60 ) Time.deltaTime); rearRight.Rotate(Vector3.right wheelRpm ( 360/60 ) Time.deltaTime);
}`