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 SGPascoe · Jun 26, 2012 at 06:41 AM · raycasttriggeraitarget

Multiple targeting help

Currently I have a script attached to my enemy characters that tries to detect where the player is. it first uses a view cone as a trigger. if the cone is triggered, it checks with a line cast to see if there are any obstacles.

If both checks succeed, the enemy will now follow the player. using:

 AIFollow.target=GameObject.Find("PlayerModel").transform;
 Debug.Log("current target is " + AIFollow.target);
 chasing = true; display.Chasing();      

However, right now. if one enemy see's the player; ALL the enemies will follow it.

It's pretty annoying, and I'm not sure how to fix the issue. If anyone has any ideas, please let me know, I'm happy to show any other code I'm using if it helps fix it.

also, as a bonus, what would be the best way to get the enemy to walk around randomly until the player arrives?

Thanks.

Comment
Add comment · Show 2
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 hijinxbassist · Jun 26, 2012 at 07:11 AM 2
Share

Are you setting target from a global standpoint? Seems like you would set it on the enemy that spots it only. Like

 if(spottedEnemy)Chase();

or something of the sort, that way only enemies that spot the player will actually follow it. It is tough to tell w.o knowing what these other references are too.. $$anonymous$$y guess is there is only one instance of the AI script and all enemies reference that single script(in which case this is intended behavior).

avatar image SGPascoe · Jun 26, 2012 at 07:31 AM 0
Share

yes you are absolutely correct. The 3 scripts governing the targeting and following are attached to, and children of the EnemyObject. I am then instancing the EnemyObject onto the level.

If it helps, I am assigning them all individual random 'gameObject.name' variables on awake.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hijinxbassist · Jun 26, 2012 at 08:18 AM

Each enemy should have its own script to govern its actions (this makes it 'think' independently from its brethren). You can instead assign that script to the new instance using

 instance.gameObject.AddComponent(AIScript);

or just add the ai script to the enemy prefab(more sensible in this case). You may need to rewrite some stuff. Remember, if you need to access that newly instantiated enemy, you can use a ref var in front of the instantiation call

 var newEnemy=Instantiate(enemy,Vector3.zero,Quaternion.identity);
 var aiScript=newEnemy.GetComponent(AIscript);

 //aiScript.player=playerVariable; //or something of the sorts
Comment
Add comment · Show 2 · 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 SGPascoe · Jun 26, 2012 at 08:41 AM 0
Share

I see! That's awesome! So I would have the current script in the project library but NOT attached to the objects. I would then place instances of my enemies, with scripts attached that simply say 'instance.gameObject.AddComponent(AIScript);' ins$$anonymous$$d of actually writing the whole script like it is currently. is this correct?

avatar image hijinxbassist · Jun 26, 2012 at 08:34 PM 0
Share

I would make an AI script and attach it to the enemy prefab. You can now instantiate the prefab(the script will already be attached to the new enemy), then you can pass info to it using the ref variable. Then they will 'think' on their own. In my example the last line(the blocked line) i pass a reference of the player to the enemy so it knows who to attack if the conditions are met. If the script you are creating enemies from is on the player itself, you can simply say something like

  newEnemy.GetComponent(AIScript).player=gameObject;//or transform

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable a target after trigger exit? 1 Answer

drone to target enemies 2 Answers

Mouse lock to target 0 Answers

Problems with raycast obstacle avoidance 1 Answer

Raycast hits a collider and halts. 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