- Home /
Random LookAt or Random Target
Hi, pls help me with this, i want my AI to random target anyone enter its area... Example: there are 2 tanks and 10 enemy tank and i want them to target anyone from those 10....the problem is that my 2 tanks is targeting same enemy.
var bullet: Transform; var damp = 4; var shootingDistance = 20; private var Target: Transform; private var shooting = 0;
function Update() {
Target = GameObject.FindWithTag("enemy").transform; //var offset = transform.position - Target.position; //if (offset.magnitude < shootingDistance) //{ //var look = Quaternion.LookRotation(Target.position - transform.position); //transform.rotation = Quaternion.Slerp(transform.rotation, look, Time.deltaTime damp); transform.LookAt(Target); var timing : int = Time.time 2; Shoot(timing); //} }
function Shoot(timing) { var offset = transform.position - Target.position; if (offset.magnitude < shootingDistance) { if(timing!=shooting) { var newBullet = Instantiate(bullet, transform.Find("bulletPoint").transform.position, Quaternion.identity);
newBullet.rigidbody.AddForce((transform.forward Random.Range(0.5,1.5)) 4500);
shooting= timing; } } }
function OnDrawGizmosSelected () { Gizmos.color = Color.red; Gizmos.DrawWireSphere (transform.position, shootingDistance); }
sorry for the mess, i was trying arrange this and i just paste here from my code
I've re-added the code tags - to add them yourself, just highlight the code and press the button 0s and 1s. $$anonymous$$akes it easier/possible to read!
Answer by · Oct 27, 2010 at 04:13 AM
- Use FindGameObjectsWithTag to return an array of all enemies.
- Use Random.Range to randomly select an enemy between the first entry in the array, and the length of the array.
You'll also either need to add a time-out to the selection, or only select a new target if your current target is null (dead, for example). Otherwise, using your current code, it will randomly select a new enemy every frame. I would suggest moving the 'find an enemy' to a new function, and only calling it when the current gameObject needs a new target (for whatever reason).
As a side-note, I'd recommend explicitly declaring the types of your code, and using tabs or (multiple) spaces to indent your code. I added code tags to your question so it displayed correctly, but it didn't make it much more readable...
If you have any specific questions about the implementation, feel free to comment back. :)
okay, here's another problem, ive used FindGameObjectWithTag, but now my tank's cannon cannot LookAt my target... T_T
Please post your code for your attempted implementation. I suggested using FindGameObject*s*WithTag (the s is very important), but without your code, I can only guess at the specific problem.
Answer by stepcross36 · Oct 31, 2010 at 10:57 AM
okay, here i got another question and problem... the code from unity reference, how do LookAt the target using this codes...
// Print the name of the closest enemy print(FindClosestEnemy().name);
// Find the name of the closest enemy function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest;
}
sorry im just getting started with this ^_^
because i tried using this, the GameObject cannot be using in LookAt(); only transform is allowed