Script C# AITargeting for the Gem Mining minigame
Hi,
I am trying to add AI competition to the tutorial gem mining game.
What I want the Ai to do: Find the gameobjects tagged as Jewels, pick them as targets and then launch the claw to collect them.
What i've tried so far was detecting the jewels via raycast and finding them via "GameObject.FindWithTag("Jewel");" but all my tries couldnt produce the result i was wishing for. Either the "claw" is not launched or it launched, but goes literally nowhere.
As I am still a noob, i would be very thankful for any ideas or explanations how to solve this problem :-)
Do you have an example of a "gem $$anonymous$$ing game" or could explain it a bit more in depth?
Answer by F_E_W · Nov 02, 2015 at 07:59 AM
Taxen0: Thanks for the reply. Its this game from unity learn section:
You are bascially shooting out a "claw" in the 2D environment, "grabbing" the jewels and dragging them to your position - its done via a vector 3 tag detection basically. Now i want an AI opponent to do the same, to add in some competition.
Answer by Taxen0 · Nov 02, 2015 at 03:53 PM
I see, I would make a AI script, and store a array of all the valid targets in it. (I have not read the code but if you already have a array like that you can re use it.)
then make the AI pick one of the targets in the array and get the position (Vector3) from it. once you have the position you just use it the same way as you do when you click.
The question is how the AI knows which object to pick, you could have it completely random, or sort the array based on value of the gem. If you want it to be even more advanced you can also raycast and make sure no crappy items are in the way.
If you don't want perfect accuracy you can also randomize a small offset and add that to the vector before you sent the claw flying =)
Thanks for the directions - its good to have some point to start at. As I've said i am still new to program$$anonymous$$g, so I would be glad if you would stay with me and my silly questions :-)
So I wrote a script for creating a List to store the jewels in. I havent gotten any further with the range detection as i didnt get the logic behind it (how does the computer know where the "$$anonymous$$er" is located?). I`ve seen some scripts using transform and mytransform, but i couldnt grasp the idea behind this either.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TargetFinder : $$anonymous$$onoBehaviour {
public List<GameObject> Targets;
void Start()
{
Targets = new List<GameObject>();
AddTargetsToList();
}
public void AddTargetsToList()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Jewel");
foreach (GameObject Jewel in go)
Targets.Add(new GameObject("Jewel"));
}
/*private void SortTargetsByDistance()
{
Targets.Sort(delegate (GameObject a1, GameObject a2)
{
return Vector3.Distance(a1.position, myTransform.position).CompareTo(Vector3.Distance(a2.position, myTransform.position));
});
}*/
void Update()
{
}
}
I also added a SelectTarget to my AI Gun:
void SelectTarget ()
{
selectedTarget = Targets[0];
targetSelected = true;
}
Now I am getting a ArgumentOutOfRangeException: Argument is out of Range Parameter Name: index - which i dont quite understand, because i thought that the generic list creates an unlimited index without any further scripting?
I am quite puzzled :-?
Your answer
Follow this Question
Related Questions
How should i go about having an object have 2 clickable areas? 2 Answers
Multi-Textured objects in unity? 1 Answer
My Brain is fogged: How do I move a networked object with user input 0 Answers
How can I make an endless zoom with endless objects appearing and an endless environment? 1 Answer
Floating Object 0 Answers