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 bpears · Jan 26, 2013 at 12:00 AM · javascriptaitag

One player tag, two players, enemy attack who ever is closer?

I'm thinking about how to go about this. Any thoughts?

Comment
Add comment · Show 13
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 iwaldrop · Jan 26, 2013 at 12:08 AM 1
Share

You could have a little algorithm that deter$$anonymous$$es it based on a few different factors; like player's health levels, or the aggregate of health and armor. You could invert that functionality based on difficulty level, so that the enemies go after the stronger of the two on easier levels.

Just some thoughts...

avatar image bpears · Jan 26, 2013 at 12:11 AM 0
Share

I was thinking more along the lines of whoever is closer. The better player shouldnt be punished for being better lol. I gues what I'm asking is how to deter$$anonymous$$e which game object is closer to the enemey object.

avatar image robertbu · Jan 26, 2013 at 01:47 AM 1
Share

If you already know the position of the two players and the enemy you can do something like:

 dist1 = Vector3.Distance(player1.transform.position, enemy.transform.position);
 dist2 = Vector3.Distance(player2.transform.position, enemy.transform.position);
 if (dist1 < dist)...

If you are asking how an enemy finds the players, there are different ways depending on the structure of the game. Start with Physics.SphereCastAll().

avatar image bpears · Jan 26, 2013 at 01:58 AM 0
Share

I'd like to stick with what I have already, since it almost works. I'm sure I'm just overlooking something. Unless the 2nd method you mentioned has noticeably better performance.

avatar image iwaldrop · Jan 26, 2013 at 02:02 AM 0
Share

Oh, well your question to me seemed phrased like you hadn't made up your $$anonymous$$d about who the enemy was going to attack and wanted input.

Since that is not the case, robertbu's answer is correct.

If you concerned with performant code, and didn't want to use Vector3.Distance(), then you could also do:

 float
     d1 = (transform.position - gameObject1.transform.position).sqr$$anonymous$$agnitude,
     d2 = (transform.position - gameObject2.transform.position).sqr$$anonymous$$agnitude;

 if (d1 < d2)...

http://docs.unity3d.com/Documentation/ScriptReference/Vector3-sqr$$anonymous$$agnitude.html

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bpears · Jan 26, 2013 at 08:11 PM

The way I had the script set up in the beginning was working how it should.

It actually ended up being a problem with one of my gameobjects transform position being off from import somehow, So I instead assigned an empty game object in its place, which has accurate transform position.

If anyone knows why imported models have messed up positions, and how to make them accurate to unity, and keep their current placement in unity, leave a comment please.

Comment
Add comment · Show 3 · 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 iwaldrop · Jan 26, 2013 at 08:15 PM 1
Share

I don't think there is a way to modify a mesh's origin from within Unity; it needs to be edited externally so that it's at the 0,0,0 (or preferred) position.

avatar image bpears · Jan 26, 2013 at 08:17 PM 0
Share

Also, a tip to debug this problem:

In update:

put Debug.Log("distance1stTarget "+distance1var+" distance2ndTarget " +distance2var)

This will update how far away the enemy is from each so you can watch in console. Put your targets in the same spot, if the distances are not equal(or at least pretty close) when you play, you have the same problem I did.

avatar image bpears · Jan 26, 2013 at 08:26 PM 0
Share

@iwaldrop good to know, thank you.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

AI Follow Player "Tag" Help 1 Answer

EnemyAI Script help 1 Answer

Distance thing with gameobject ? 4 Answers

Make player not be seen by AI, when player in foilage and shadows. 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