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 D3m0nE · Apr 06, 2013 at 07:49 PM · enemybulletshoot

Bullet Shoot At Enemy Position

Hello Everyone

i use this FireScript :

                         fireRate = fireRate - extrafirerate;
             nextFire = Time.time + fireRate;
             launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0);    
             GameObject go = Instantiate(blaster, launchPosition,  Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
                                                                 transform.eulerAngles.y, 0)) as GameObject;
             


and in The Blaster[Bullet] prefab Added BlasterScript

     myTransform.Translate(Vector3.up * 100 * Time.deltaTime);
         
     Destroy(gameObject,1.5f);


The cameraHeadTransform = SpawnPoint its inside the player

i just want : When I Shoot, the bullet Go To Closest[Distance] Enemy With Tag "Enemy"

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
Best Answer

Answer by janzdott · Apr 06, 2013 at 08:45 PM

 // find all enemies
 GameObject enemies = GameObject.FindGameObjectsWithTag ("Enemy");
     
 //this will find the closest enemy to your launchPosition
 int closestIndex = 0;
 float closestDistance = Mathf.Infinity;
 float tempDistance;
 for (int i = 0; i < enemies.Length; i++) {
     tempDistance = Vector3.Distance (launchPosition, enemies[i].transform.position);
     if (tempDistance < closestDistance) {
         closestDistance = tempDistance;
         closestIndex = i; 
     }
 }
 GameObject closestEnemy = enemies[closestIndex];
     
 //this is your bullets direction
 Vector3 bulletDirection = closestEnemy.transform.position-launchPosition;
 
 //instantiate your bullet with this rotation.
 Quaternion bulletRotation = Quaternion.LookRotation(bulletDirection, Vector3.up);


Comment
Add comment · Show 5 · 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 D3m0nE · Apr 06, 2013 at 09:38 PM 0
Share

Thanks but not working :)

I dont know, its just spawn the bullet very far away and under the ground

.. hold on second ,,

i should attack the script to the bullet or inside my firescipt

anyway i tried both. i guess he got problem in finding the Tag

avatar image janzdott · Apr 06, 2013 at 11:01 PM 0
Share

Sorry, I didn't write it to work if you copied and pasted it directly into your code. I edited it, hopefully you can get it to work now. But your bullet is going to go up, unless you change your bullet to move Vector3.forward ins$$anonymous$$d of Vector3.up

avatar image D3m0nE · Apr 06, 2013 at 11:48 PM 0
Share

umm Okey.

i guess i should change

 GameObject enemies = GameObject.FindGameObjectsWithTag ("Enemy");
 
 To :
 
 GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
 

and

  GameObject go = Instantiate(blaster, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
 transform.eulerAngles.y, 0)) as GameObject;
 
 
 //to :
 
 GameObject go = Instantiate(blaster, launchPosition,bulletRotation) as GameObject;


and in the bulletscript

 myTransform.Translate(Vector3.up * 100 * Time.deltaTime);
 
 //to
 
 myTransform.Translate(Vector3.forword * 100 * Time.deltaTime);
avatar image D3m0nE · Apr 07, 2013 at 12:08 AM 0
Share

Its Works Like Charm,, thank you

but there is little problem that he ai$$anonymous$$g on the leg..

i fixed it by adding vector3

 Vector3 bulletDirection = closestEnemy.transform.position-launchPosition+ new Vector3(0,1,0);

its works but i'm not sure if its right way xD

thx again

avatar image janzdott · Apr 07, 2013 at 01:44 AM 0
Share

Good I'm glad it worked, and sorry about the typo. Its hard writing code on a phone haha

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

12 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

Related Questions

Hit enemy life 0 Answers

Shoot Bullet Delay Enemy 0 Answers

No collision on trigger and character controller 1 Answer

Problem with enemy shooting 4 Answers

Enemy Instantiating one bullet 3 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