- Home /
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.
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.
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);
}
Haha, yea. I've already got the missile movement down pat. It's not hard xD.