- Home /
Using Raycast to "Lock On"
I'm trying to use a Raycast to Lock on to an enemy but it doesn't seem to be working. Also is it possible to set up an array or list of targets it can lock onto?
function Update(){ var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 100)) {
if (target.gameObject.tag == "Enemy"){
transform.LookAt(target);
}
}
}
And if possible can someone explain a method of shifting through targets and cancelling the lock on with a button?
Answer by skovacs1 · Oct 28, 2010 at 02:44 PM
- You store a hit variable, but you never use it.
- What is target? When do you set it and what do you set it to?
Whether your raycast hit or not, this code wouldn't do anything.
You probably want something more like:
function Update(){
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 100)
&& hit.transform.gameObject.tag == "Enemy")
transform.LookAt(hit.transform);
}
If you wanted an array of available targets you would create an array of available targets - I think the question is itself the answer. Perhaps you meant to ask something else - maybe about how to set or fill an array of targets or how to use such an array?
var targets : GameObject[]; //You can fill this in the editor too
You could initialize it with something like:
function Start() {
targets = GameObject.FindGameObjectsWithTag("Enemy");
}
If your array is going to change at run-time, you might in stead consider an Array object and use Array.Add or Array.RemoveAt or Array.Push or Array.Pop or Array.Shift or Array.Unshift to change the content of the array as you need.
var targets : Array; //This will not serialize to the editor
function Start() { targets = new Array(GameObject.FindGameObjectsWithTag("Enemy")); }
To check if your hit object is in the array in either form, you would have to loop through the array.
for(var go : GameObject in targets)
//do something with go like if(go == hit.transform.gameObject)...
But can you explain how to shift through the targets with a button press?
shift through? I assume you mean not to use the raycast method, but to simply lock onto a specific target from the array. That's fairly simple. Store an int variable outside of your functions as your current target and then increment it on button press to change your current target.
var targets : Array; private var current : int = 0; private var locked : boolean = false; function Start() {targets = new Array(GameObject.FindGameObjectsWithTag("Enemy"));} function Update() {if(Input.GetButtonDown("Lock")) locked = !locked; if(locked) {if(Input.GetButtonDown("Next Target")) current++; if(Input.GetButtonDown("Previous Target")) current--; current = $$anonymous$$athf.Clamp(current, 0, targets.length-1); if(targets.length > 0) transform.LookAt(targets[current].transform);}}
To lock onto targets by proximity, you would have to sort the array somehow or write a function that finds the nth closest object in the array. Sorting might be a bit easier in c# with more complex containers and methods to sort with. Odds are if you go this route, you'd write your own findNthClosestTarget function and call that with current because as you play, the targets may get closer and farther. Also, when using the array method, make sure to remove destroyed enemies and to add new enemies in order to ensure your target array is current.