Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by F_E_W · Nov 02, 2015 at 04:38 AM · objectsaimingtargeting

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 :-)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taxen0 · Nov 02, 2015 at 07:43 AM 0
Share

Do you have an example of a "gem $$anonymous$$ing game" or could explain it a bit more in depth?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by F_E_W · Nov 02, 2015 at 07:59 AM

Taxen0: Thanks for the reply. Its this game from unity learn section:

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-jewel-miner-game

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 =)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image F_E_W · Nov 03, 2015 at 12:17 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges