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 Black Widow 1 · Dec 03, 2010 at 08:58 AM · enemylockon

Enemy LockOn and Shoot

Hi, I have these t Scripts that work well seperately but i cant combine them and make them work the same.

i want to when hit fire button actually lock on enemy and target which happens when seperate.

I would also like to have the lock remain on until i mous down elswhere.

Thanks

EnemyLockOn

var target: Transform; var rotationSpeed = 3; //speed of turning var myTransform : Transform; //current transform data of this enemy

function Update () { // check distance to target every frame: var distance = (target.position - myTransform.position).magnitude; var hit : RaycastHit;

 if(Physics.Raycast(transform.position, transform.forward, hit, 100)
    && hit.transform.gameObject.tag == "Enemy")
     var enemy = GameObject.FindWithTag("Enemy");
 if(enemy != null)
     if(Input.GetButton("Fire1")) {
         transform.LookAt(hit.transform);
         Debug.DrawLine(transform.position,target.position, Color.yellow);

         //rotate to look at the player    
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,   
             Quaternion.LookRotation(target.position - myTransform.position),
             rotationSpeed*Time.deltaTime); 
     }

}

and

IsTargeting

var isTargeting : boolean; var speed : float = 5;

function Update () { if(Input.GetButton("Fire1")) var enemy = GameObject.FindWithTag("Enemy");

 if(enemy != null) {
     var enemyLocation = enemy.transform;
     transform.LookAt(enemyLocation);
 }

}

Comment
Add comment · Show 4
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 Proclyon · Dec 03, 2010 at 09:17 AM 0
Share

Could you reformat the question and split the scripts in visible components? I have no idea what goes where like this and I'm sure the other people that want to see this question answered struggle aswell. Thanks in advance! (just use the edit option and the post-previewer to keep formatting clean)

avatar image Black Widow 1 · Dec 03, 2010 at 11:15 AM 0
Share

Hi, I have a Player and as a child of the player i have 1 weapon object that has 4 weapon scripts that instantiate bullets and grenades that i can switch between using keys 1,2,3 & 4. I have put both of these scripts on the weapon object. What i am trying to do is on key down lock onto a target and fire at it but when i click elsewhere i break lock. Hope this makes sence. I am just playing with a few blocks to try to get the concept right as i am new to this.

Thanks

avatar image skovacs1 · Dec 03, 2010 at 09:00 PM 0
Share

This code is terrible. In both scripts you declare the variable enemy in a narrower scope than where you try to access it. Why on earth are you calling GameObject.FindWithTag to find a GameObject tagged "Enemy" only when your raycast hit a GameObject tagged "Enemy"? You already have it so you don't need to go and find it.

avatar image Black Widow 1 · Dec 04, 2010 at 03:32 AM 0
Share

I am very new at this and still got a long way to go, obviously. Thanks for the advice i will have a look. thats why i was trying to make one out of the 2.

0 Replies

· Add your reply
  • Sort: 

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

lock ontro enemy 1 Answer

Locking onto Multiple AI's 1 Answer

Lock onto a target 1 Answer

Scripts Destroys all Objects its Attached to! 2 Answers

Enemies spawn on top of each other 2 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