- Home /
Need help with a Third-Person Styled Lock-On System
I want to implement a Lock-on/Trigger system that will let the player find the closest enemy, have the player facing the enemy, and having him attack towards the enemy he is targeted to. I also want to display a ring or a targeting icon on the enemy that is targeted.
How can I make it to where I press the targeting button and have it select the closest enemy and make it "the target"?
I had suggestions to where I give the enemies tags called "Enemy" (which i did) and store them into an array, but that wasn't really enough help :P
Also, after defeating the enemy that I manually set as the target in the inspector, I get this error
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
because of the code that allows the player to face the enemy when attacking:
transform.LookAt(Vector3(theTarget.transform.position.x, transform.position.y, theTarget.transform.position.z));
I think 'Transform' is referring to 'theTarget' being destroyed
How do I get the script to check if "theTarget" is null after destroying it or how do I disable the LookAt() line if enemies don't exist on the map?
I think you should break it down and ask a more specific question rather than saying "I want feature X" here is my code.
I did: "a Lock-on/Trigger system that will have the player facing the enemy and having him attack towards the enemy he is targeted to."
Can't really get any more specific than that other than saying: "I want to get the player to look at another object." and "I want the player to dash towards the other object"
A targeting system allows a players actions to be focused toward the object he is targeting at, figured any other details in the question would be pointless.
I provided the code to show what I have completed and an attempt to demonstrate the implementation I was heading towards.
It's good that you removed the massive block of code from your question. However your questions are still in the context of your code which makes it difficult for readers to understand. If you boil the problem down to a simple specific problem without the context such as: "How do I make an object lookat another object only rotating about Y-axis?" That can be answered easily (and already has been)
Okay, I just chopped the question down a bit more. How is it now?
BTW, thanks to your last comment, I found I bit of the answer I was looking for: http://forum.unity3d.com/threads/49471-LookAt-to-rotate-only-on-Y-axis
Answer by SrBilyon · Dec 27, 2010 at 11:56 AM
Solved everything! (Not by myself entirely, this is a bit embarrassing)
This is the script I wrote thanks to handsomePATT from the forums and an answer skovacs1 gave a while back to another answer (got to give credit where credit is due)
//================================================ //Lock On Script //================================================ private var current : int = 0; private var locked : boolean = false; var playerController : ThirdPersonController ; var enemyLocations : GameObject[]; var closest : GameObject; var activeIcon : Transform; //Current targeted enemy indicator
function Update() {
var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
if (closest != null && locked)
{ activeIcon.active = true; activeIcon.transform.position.y = (closest.transform.position.y+1); activeIcon.transform.position.x = (closest.transform.position.x); activeIcon.transform.position.z = (closest.transform.position.z); } else {
activeIcon.active = false; }
if(Input.GetButtonDown("Lock"))
{
//Looks for the closest enemy
FindClosestEnemy();
locked = !locked;
}
if(locked)
{
//If there aren't any enemies (or the player killed the last one targeted) make sure that the lock is false
if (!closest)
{
activeIcon.active = false;
locked = false;
closest = null;
}
if (playerController.isAttacking)
transform.LookAt(Vector3(closest.transform.position.x, transform.position.y, closest.transform.position.z));
}
}
function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); //var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in enemyLocations) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
Answer by Jean-Fabre · Dec 27, 2010 at 07:32 AM
Hi,
Not Actually coming up with a ready answers ( sorry :) ), but just some directions that might be useful.
It looks to me you need to add another hierarchy level between the controller and the player by adding one gameObject inbetween the two, that gameobject will then allow to control the rotation independently from the controller.
This allow you to have the controller moving and looking at its target while you have the inbetween gameObject looking somewhere else.
Anyway, I think for someone to really come up with an answer, you should make a small example, exposing the issue, then I would happily have a look, mess with it to achieve the desired effect. This is the most effective way in my opinion to solve such precise case.
Bye,
Jean
Alright, I revised my question to reflect on what I actually need.
isn't part of the answer in the error you get? simply check or build a flag to know whether you should lookat your ennemy in a certain way or not. Or do you actually want to know how to check if a variable is null? Also your revision seems now to boil down to checking where your enemy ares and picking up the closest. am I correct? If that's the case , I can build a quick scene showing how to pick the closest gameobject either from the mousepointer or from an actual position in space. Would that help you progress?