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 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.

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 do I find a player for AI to target when spawning a player in? 0 Answers

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

How to change a game objects tag after it has been randomly selected? 0 Answers

Array transform.position from another script 1 Answer

Enemy Patrol AI is sideways 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