Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Alter · Oct 30, 2013 at 07:07 AM · enemy aitargeting

Targeting system HELP please

hi guys, I'm having a bit of trouble getting my enemies to target my characters. On a single character it's fine, but when I switch character mid stage the enemies still focus on where the first character was/is located. Think of it like Mega Man only swapping characters other than weapons.

The script attached is what I have so far and any help is greatly appreciated. Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public GameObject[] targetAll;
     public int rotationSpeed;
     public int stopDistance;
     public int sightDistance;
     public int turnDistance;
 
     
     private float moveSpeed;
     public float moveSpeedAttack;
 
     
     //float souls = PlayerPrefs.GetInt("Souls");
     
     
     private Transform myTransform;
     
     void Awake(){
         myTransform = transform;
         targetAll = GameObject.FindGameObjectsWithTag("Player");
         
         GameObject go = GameObject.FindGameObjectWithTag("Player");    
         target = go.transform;
     }
 
     // Use this for initialization
     void Start () 
     {
     }
     
     // Update is called once per frame
     void Update () 
     {
         targetAll = GameObject.FindGameObjectsWithTag("Player");
         float distance = Vector3.Distance(target.transform.position, transform.position);
         
         Debug.DrawLine(targetAll.position, myTransform.position, Color.red);
         
     if(Vector3.Distance(target.position, myTransform.position) < sightDistance)
         {        
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
             moveSpeed = 0;
         }
         
         
         if(Vector3.Distance(target.position, myTransform.position) < stopDistance)
         {
         moveSpeed = 0;
         }
         
         if(Vector3.Distance(target.position, myTransform.position) > stopDistance)
         {
         moveSpeed = moveSpeedAttack;
         }
         
     if(Vector3.Distance(target.position, myTransform.position) < turnDistance){
         //Look at target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
             }            
     }
 }


PS: I'm getting this error if it helps- error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for position' and no extension method position' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Oct 30, 2013 at 07:27 AM

I would use a static Transform.

This transform contains the active player you use it just like you are using target now.

You can then remove everything about targetAll.

In your control script, you should a method that swap players. When you swap just make the static var receives the new Transform. That is it.

If you want to go for a more OOP style:

You can have a game manager which has a Transform for activePlayer. This variable is passed to the enemy on creation so in the Start of the enemy script you access the GM and find that variable.

Your control script updates that variable in the GM which indirectly will also update all enemies at once since it is just a reference. And no worries about performance, you are dealing with references so it is light and fast.

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 Alter · Oct 30, 2013 at 07:50 AM 0
Share

Thanks fafase, I'll give it a go :)

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

15 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

Related Questions

How do I flip an enemy that's automatically walking and targeting the player? 1 Answer

How to make unit priority targeting system? 2 Answers

How to make a game object detect if any of multiple game objects are in range 2 Answers

Help with Enemy Ai not going back to patroling after attack ,help with enemy ai not going back to regular movement after attack 0 Answers

Targeting the GameObject I collide with? 1 Answer


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