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 Vincenzo Albery · Aug 10, 2010 at 09:22 AM · distanceradius

Check Radius Problem

Hello Everybody,

I need your little help. I have a script I would check around the character and i found the closest enemy with this script. This script working good but this script check only closest enemy when character is spawn how can i check it everytime ? Example how can i check my radius when my chracter is walking

My Code is here ;

private var startPoint: ClosestEnemySystem; function Start(){ ClosestEnemy(); startPoint= ClosestEnemy().GetComponent(ClosestEnemySystem); //Wonder= ClosestEnemy().GetComponent(ClosestWonderSystem); startPoint.isStart=true; }

function ClosestEnemy() : GameObject {

var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Soldier"); var closest : GameObject; //var distance = Mathf.Infinity; var distance : float; distance = 30;

var position = transform.position;

for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } function OnDrawGizmos () { if (startPoint != null) { Gizmos.color = Color.red; Gizmos.DrawLine (transform.position, startPoint.gameObject.transform.position); } }

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

2 Replies

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

Answer by Badger · Aug 10, 2010 at 10:14 AM

Are you sure there are enemies within 30 units? If not, closest will not be initiated by this imho.

Also, why would you want to replace Infinity with a set distance? It doesn't reduce the number of loops on this function, as it still loopts through all enemies doing a distance calculation. Might be a solution to check in the end if the distance is smaller than 30, if not, set closest to player position (or whatever else you'd like) :)

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 Vincenzo Albery · Aug 10, 2010 at 10:49 AM 0
Share

Thank you for your reply but i developing a 3d tower defence game i controlling my characters radius and if a object in my radius i will attack this object. If i want make a # stable tower it is no problem but But my characters is walking :) How can i control my radius when my character when they walking ?

avatar image Badger · Aug 10, 2010 at 12:31 PM 1
Share

I see, i reread the question.

Put the call to ClosestEnemy() in the update function ins$$anonymous$$d of the start one. That should have it checked every time around i think

avatar image Bampf · Aug 11, 2010 at 03:05 AM 0
Share

I think Badger spotted the basic problem. I didn't realize that you were only calling the ClosestEnemy method in Start(). You need to put logic somewhere (such as Update) that gets called more than once. Or, switch to colliders and the physics loop will do that for you.

avatar image
1
Best Answer

Answer by Bampf · Aug 10, 2010 at 11:06 AM

I can't spot anything wrong with your code off the top of my head. Is that script attached to the moving game object?

You should consider using sphere colliders instead. They generally require a lot less code, and you can see them in the Unity editor window so it's very easy to visualize when things collide.

As a bonus, the physics engine is optimized to avoid doing things like test every object in the scene the way your loop has to.

You can also use raycasting with the colliders, so you can do things like figure out if a laser beam hits an enemy, or if the enemy can see the tower behind another object.

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 Vincenzo Albery · Aug 10, 2010 at 11:09 AM 0
Share

Thank you for your reply i draw gizmos with this code but script draw gizmos only when object spawned :(

function OnDrawGizmos () { if (startPoint != null) { Gizmos.color = Color.red; Gizmos.DrawLine (transform.position, startPoint.gameObject.transform.position); }

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

No one has followed this question yet.

Related Questions

How to make this script find distance inside radius? 1 Answer

Math Question: Get EnemyObject to NavMeshDestination x Units from Player 1 Answer

How to apply explosion/magic/grenade damage to detect objects (enemies) within a given radius, and apply AOE damage to each enemy based on its distance? 2 Answers

resizing spherecast radius over distance? 4 Answers

Smooth Orbit Round Object with Adjustable Orbit Radius 5 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