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 stepcross36 · Oct 27, 2010 at 04:00 AM · randomtargettargeting

Random LookAt or Random Target

Hi, pls help me with this, i want my AI to random target anyone enter its area... Example: there are 2 tanks and 10 enemy tank and i want them to target anyone from those 10....the problem is that my 2 tanks is targeting same enemy.

var bullet: Transform; var damp = 4; var shootingDistance = 20; private var Target: Transform; private var shooting = 0;

function Update() {

Target = GameObject.FindWithTag("enemy").transform; //var offset = transform.position - Target.position; //if (offset.magnitude < shootingDistance) //{ //var look = Quaternion.LookRotation(Target.position - transform.position); //transform.rotation = Quaternion.Slerp(transform.rotation, look, Time.deltaTime damp); transform.LookAt(Target); var timing : int = Time.time 2; Shoot(timing); //} }

function Shoot(timing) { var offset = transform.position - Target.position; if (offset.magnitude < shootingDistance) { if(timing!=shooting) { var newBullet = Instantiate(bullet, transform.Find("bulletPoint").transform.position, Quaternion.identity);

newBullet.rigidbody.AddForce((transform.forward Random.Range(0.5,1.5)) 4500);

shooting= timing; } } }

function OnDrawGizmosSelected () { Gizmos.color = Color.red; Gizmos.DrawWireSphere (transform.position, shootingDistance); }

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 stepcross36 · Oct 27, 2010 at 04:10 AM 0
Share

sorry for the mess, i was trying arrange this and i just paste here from my code

avatar image · Oct 27, 2010 at 04:17 AM 0
Share

I've re-added the code tags - to add them yourself, just highlight the code and press the button 0s and 1s. $$anonymous$$akes it easier/possible to read!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by · Oct 27, 2010 at 04:13 AM

  1. Use FindGameObjectsWithTag to return an array of all enemies.
  2. Use Random.Range to randomly select an enemy between the first entry in the array, and the length of the array.

You'll also either need to add a time-out to the selection, or only select a new target if your current target is null (dead, for example). Otherwise, using your current code, it will randomly select a new enemy every frame. I would suggest moving the 'find an enemy' to a new function, and only calling it when the current gameObject needs a new target (for whatever reason).

As a side-note, I'd recommend explicitly declaring the types of your code, and using tabs or (multiple) spaces to indent your code. I added code tags to your question so it displayed correctly, but it didn't make it much more readable...

If you have any specific questions about the implementation, feel free to comment back. :)

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 stepcross36 · Oct 29, 2010 at 04:58 AM 0
Share

okay, here's another problem, ive used FindGameObjectWithTag, but now my tank's cannon cannot LookAt my target... T_T

avatar image · Oct 29, 2010 at 05:15 AM 0
Share

Please post your code for your attempted implementation. I suggested using FindGameObject*s*WithTag (the s is very important), but without your code, I can only guess at the specific problem.

avatar image
0

Answer by stepcross36 · Oct 31, 2010 at 10:57 AM

okay, here i got another question and problem... the code from unity reference, how do LookAt the target using this codes...

// Print the name of the closest enemy print(FindClosestEnemy().name);

// Find the name of the closest enemy function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one 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;
}

sorry im just getting started with this ^_^

because i tried using this, the GameObject cannot be using in LookAt(); only transform is allowed

Comment
Add comment · 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

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

Track objects in camera view using GUI? Like a HUD for flight sim lockon targeting. 1 Answer

Tower Target Random Tank 1 Answer

Targeting an enemy trouble 1 Answer

How can I rotate a shooting-practice target 180 degrees with a timer in C#? 1 Answer

Circle strafing/Z-Targeting 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