• 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 SeraphimIIIIIIIIII · Jun 03, 2016 at 05:52 PM · gameobjecttransformarrayaitag

Need assistance with an primitive AI.

I'm trying to finish a project in one of my classes, but the bug script I've created is not working(Used to work before I added new protocols). I'm trying to make it so that a float, variable, or some form of Array holds account of all objects with the tag "Crop", during this process it should gather their transforms and then sort out all "Crop" distances, then have the script select the closest tagged GameObject to charge at. I have tried all I can from knowledge and gathered knowledge, but I can't figure this problem out.

 using UnityEngine;
 using System.Collections;
 
 public class BugAI : MonoBehaviour {
     
     public float fpsTargetDistance;
     public float enemyLookDistance;
     public float attackDistance;
     public float enemyMovementSpeed;
     public float damping;
     public float[] targets;
     public Transform selectedTarget;
     Rigidbody theRigidbody;
     public float[] List;
     
     
     
     
     
     
     
     // Use this for initialization
     void Start () {
         theRigidbody = GetComponent<Rigidbody> ();
         selectedTarget = null;
         foreach (float target in targets) {
             AddAllEnemys ();
         }
     }
     
     public void AddAllEnemys(){
         float go = GameObject.FindGameObjectsWithTag ("Crop");
         
         foreach (float enemy in go) {
             AddTarget (enemy.GetTypeCode(Transform));
         }
         
     }
     
     public void AddTarget (Transform enemy)
     {
         var Amm = targets [0];
         Amm = new float(List[0] + 1);
     }
     
     private void SortTargetsByDistance()
     {
         targets.Sort (delegate(Transform t1, Transform t2) {
             return Vector3.Distance (t1.position, transform.position).CompareTo (Vector3.Distance (t2.position, transform.position));
         });
     }
     
     private void TargetEnemy()
     {
         var Kel = 0;
         if (selectedTarget == null) {
             SortTargetsByDistance ();
             selectedTarget = targets [0];
         } else {
             Kel = 1;
         }
         selectedTarget = targets [Kel];
     }
     // Update is called once per frame
     void FixedUpdate () {
         fpsTargetDistance = Vector3.Distance (selectedTarget.position, transform.position);
         if (fpsTargetDistance < enemyLookDistance) {
             lookAtPlayer();
             TargetEnemy ();
             print ("Look at Playa please!");
         }
         if (fpsTargetDistance < attackDistance) {
             attackPlease ();
             print ("ATTACK!");
         } 
         else {
             print ("sad face");
         }
         
     }
     
     void lookAtPlayer () {
         if (fpsTargetDistance > attackDistance) {
             Quaternion rotation = Quaternion.LookRotation (selectedTarget.position - transform.position);
             transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
             theRigidbody.AddForce (transform.forward*enemyMovementSpeed/2);
         } else {
             Quaternion rotation = Quaternion.LookRotation (selectedTarget.position - transform.position);
             transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
         }
     }
     
     void attackPlease () {
         theRigidbody.AddForce (transform.forward*enemyMovementSpeed);
         
     }
 }

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

92 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 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 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 to change a game objects tag after it has been randomly selected? 0 Answers

Enemy Patrol AI is sideways 0 Answers

Array transform.position from another script 1 Answer

How to get the initial position of all gameobjects in an array unity 1 Answer

How do I find a player for AI to target when spawning a player in? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges