Hi All
I am just new to Unity and Scripting but I have been using a lot of tutorials to get the basics and I am currently trying my hand at a space RTS, I have got most of the basics, resources, unit and structure building, basic click to move (I struggled with this for a while trying to figure out movement in 3D space). So I would say I am progressing very slowly, an RTS seem to be a much more complex beast than an FPS or RPG to create :-).
I am struggling on AI attack behaviors for my ships.
This is what I have so far.
using UnityEngine;
using System.Collections;
public class AttackAi : MonoBehaviour {
float moveSpeed = 50f;
float randomx;
float randomy;
float randomz;
float distance;
float distanceT;
int switchTarget;
public bool AITarget;
public bool randomTarget;
// Use this for initialization
public Transform enemyTarget;
Transform newTarget;
Vector3 destinationPosition;
void Start () {
destinationPosition = enemyTarget.position ;
switchTarget = 0;
AITarget = true;
randomTarget = false;
}
// Update is called once per frame
void FixedUpdate () {
randomx = Random .Range (-500, 500);
randomy = Random .Range (-500, 500);
randomz = Random .Range (-500, 500);
if (AITarget == true) {
distance = Vector3 .Distance (transform .position, enemyTarget .position);
}else
{
distanceT = Vector3 .Distance (transform .position, destinationPosition);
Debug.Log (distanceT );
}
switch (switchTarget) {
case 1:
Vector3 positionx = new Vector3 (enemyTarget .position .x+randomx, enemyTarget .position .y, enemyTarget .position .z);
destinationPosition = positionx;
switchTarget = 0;
break;
case 2:
Vector3 positiony = new Vector3 (enemyTarget .position .x, enemyTarget .position .y+randomy, enemyTarget .position .z);
destinationPosition = positiony;
switchTarget = 0;
break;
case 3:
Vector3 positionz = new Vector3 (enemyTarget .position .x, enemyTarget .position .y, enemyTarget .position .z+randomz);
destinationPosition = positionz;
switchTarget = 0;
break;
}
if (distance < 300 && AITarget == true)
{
switchTarget = Random .Range (1,3);
AITarget = false;
randomTarget = true;
}
else if (distanceT < 50 && randomTarget == true)
{
destinationPosition = enemyTarget.position ;
AITarget = true;
randomTarget = false;
}
Quaternion targetRotation = Quaternion.LookRotation (destinationPosition - transform.position);
transform.rotation = Quaternion .Slerp (transform .rotation, targetRotation , Time.deltaTime);
transform.position = Vector3.MoveTowards (transform.position, destinationPosition , moveSpeed * Time.deltaTime);
}
}
Now this code does work but not very good, it works OK I guess for a fighter attacking a slow moving capital ship, but for fighter v fighter its just useless. Also as this is based on the transform position of the enemy, it doesnt take into consideration the mesh extents of the ship so sometimes my ships go through the object rather than avoid it.
The main issues with my script is there is no smooth angle of change towards the random target, the random isnt giving me the best effect for a strafing run and at the moment my ships have no real yaw or pitch.
So looking for some help to refine and improve upon my script, to give my AI some life to them, this script is for fighters or fast moving ships only, I am currently working on a capital ship script which uses more of a RotateAround so it can broadside with its weapons.
Cheers
Druid