• 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 isfrseirra259 · Oct 16, 2014 at 12:57 PM · javascriptraycastmissilelocking

How to make a missile locking system?

How does one create a locking system where if you look at a certain object, the lock-on timer increases, but as soon as you look away or look at a different object, the lock-on timer resets to zero. When the lock-on timer reaches a certain point, the target becomes locked, and any missile fired will curve towards that object. The lock will be maintained for a set amount of time, even if you look away or look at a different object. And if the lock-on is broken, a missile fired during the time when a lock on was established will still maintain what was previously locked. Pretty much a "fire-and-forget" kind of system.

Basically what I'm having trouble is how do you check that what you're looking at in the current frame is the same as what you were looking at in the previous frame? It's that "check to see if you're still looking at something, or you've looked away" that I can't seem to do.

I'm using Raycasts to do the "looking" part, but I don't know how to perform that check if one uses Raycasts. Perhaps Raycasts aren't the answer?

Answer in javascript please.

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

Answer by HarshadK · Oct 16, 2014 at 01:28 PM

For

check to see if you're still looking at something, or you've looked away

You can maintain a variable that stores the instance ID of the object you are currently looking at. You can decide the object you're looking at using Raycast.

Something like the script below. Note that this is not a working script, it is just to illustrate the logic:

var currentObjectInstanceID : int = null;

 function Update()
 {
     var fwd = transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast (transform.position, transform.forward, hit)) {
         // You are looking at one of the target objects
         hit.collider.gameObject.tag == "LookAtTargets"{
             // You were previously not looking at any object and now you are looking at an object which is one of the target objects
             if(currentObjectInstanceID == null)
             {
                 currentObjectInstanceID = hit.collider.gameObject.GetInstanceID();
             }
             // You were previously looking at the same object
             else if(currentObjectInstanceID == hit.collider.gameObject.GetInstanceID())
             {
                 // You are currently looking at the same object. Keep adding to the lock timer of this object
             }
             // You were previously looking at different object but you are still looking at another target object
             else if(currentObjectInstanceID != hit.collider.gameObject.GetInstanceID())
             {
                 // You are now looking at different target. Start lock timer for this object.
             }
         }    
         // Or you are not looking at any of the target object
         else
         {
             // You are not looking at any target objects
             currentObjectInstanceID = null;
             
             // Reset the timer to zero for your target object
         }
     }
 }

And you can use the method suggested by @N1warhead for making it follow the target after it is fired.

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 isfrseirra259 · Oct 16, 2014 at 08:36 PM 0
Share

Thanks, helped heaps.

avatar image
1

Answer by N1warhead · Oct 16, 2014 at 01:07 PM

It's actually very simple.

I'm not gonna get into how to make the missle move realisticly as that deals with Slerps, which aren't hard, but tweaking them the way you want is lol.

Do something like this (in C#) Add this to your PROJECTILE.

 public GameObject target;
 public float Speed = 40.0f; // Change to your liking.
 
 void Update(){
 
 transform.LookAt(target);
 tranform.Translate(Vector3.forward * Speed * Time.deltaTime);
 }
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 isfrseirra259 · Oct 16, 2014 at 08:30 PM 0
Share

Haha, yea. I've already got the missile movement down pat. It's not hard xD.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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 avatar image avatar image avatar image avatar image avatar image 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

raycast to object, load wrong script!? 2 Answers

Launching a Projectile to a Raycast 1 Answer

Raycasting 1 Answer

How to get to know a mesh' faces normals? 0 Answers

AndoidTouchControllJavaRaycast 0 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