- Home /
Raycast and timer to select and lock-on to targets
I'm creating a mechanic (like a fighter jet simulation) where the player holds down the fire button and locks on to enemies. When the crosshair is on an enemy, the target is selected, if he keeps the crosshair on the enemy for a set amount of time (lock-on time) the target is added to an array (lockedTargets). I'm having trouble.
More EDITS!
Here is the current code:
public class TargetAttack : MonoBehaviour {
public float attackRange = 7f;
public float lockTime = 2.0f;
public int numLocks = 0;
public int maxLocks = 5;
public GameObject currentTarget = null;
public string currentTargetName;
public float running_time = 0f;
// Use this for initialization
void Start ()
{
running_time = 0f;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButton ("Fire1"))
{
EyeTarget ();
}
}
void EyeTarget ()
{
Ray ray = Camera.main.ScreenPointToRay (new Vector2 ((Screen.width / 2), (Screen.height / 2)));
RaycastHit hit;
if (Physics.Raycast (ray, out hit, attackRange))
{
if(hit.collider.gameObject.tag == "Enemy")
{
currentTarget = hit.collider.gameObject;
currentTargetName = hit.collider.name;
//Debug.Log(hit.collider.name);
Debug.DrawLine (ray.origin, hit.point);
running_time += Time.deltaTime * 1;
if( running_time >= lockTime && numLocks < maxLocks)
{
numLocks++;
Debug.Log("Locked on");
running_time = 0f;
}
}
else
{
ResetTimer();
running_time = 0f;
}
}
}
void ResetTimer()
{
running_time = 0f;
Debug.Log("resetting timer");
}
}
1) The lock-on timer. I got the timer to work as I'd like, though I'd like it to stop counting after all the lock-ons are acquired. It doesn't.
2) The timer only resets on collision. This is still an issue. I want the timer to reset when there is no collision with anything, because that means the player has stopped tracking the target, or the target has moved out of range. However, in the current implementation, the timer only resets when I hit the floor with the ray (because it collides but it is not tagged "enemy"). When I shoot off into the sky, the timer stops but remains where it was. Which means it can be resumed if I bring the cursor back onto an enemy. How can i get it to reset by default and count only on contact?
3) Also it seems like I don't need to have an if loop for line 36 (" if (Physics.Raycast (ray, out hit, attackRange)) ") but when I simply use Physics.Raycast (ray, out hit, attackRange); I get odd errors when targeting ("
NullReferenceException: Object reference not set to an instance of an object TargetAttack.EyeTarget () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/TargetAttack.cs:38) TargetAttack.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/TargetAttack.cs:27)
"). I think there's something wrong with my order of operations or the loop type I'm using but i'm too much of a noob to figure out whats going on.
Answer by christoph_r · Aug 10, 2014 at 05:41 PM
Check in
if(hit.collider.gameObject.tag == "Enemy")
also if the max number of lock-ons is reached.Call your ResetTimer also in an else order (after the Raycast check). So if there's no collision, it will reset the timer.
No, that piece of code makes sense. And it's not a loop anyway, it's just a single check that gets called every frame (due to being called in
Update()
).
Hope that helps.
Thanks! Your comments are spot on. I made the changes and it works great! Just for those that may need something similar, the working code is:
void EyeTarget ()
{
Ray ray = Camera.main.ScreenPointToRay (new Vector2 ((Screen.width / 2), (Screen.height / 2)));
RaycastHit hit;
if (Physics.Raycast (ray, out hit, attackRange)) {
Debug.DrawLine (ray.origin, hit.point);
if (hit.collider.gameObject.tag == "Enemy" && numLocks < maxLocks) {
currentTarget = hit.collider.gameObject;
currentTargetName = hit.collider.name;
//Debug.Log(hit.collider.name);
Debug.DrawLine (ray.origin, hit.point);
running_time += Time.deltaTime * 1;
if (running_time >= lockTime && numLocks < maxLocks) {
numLocks++;
Debug.Log ("Locked on");
running_time = 0f;
}
}
}
else
{
ResetTimer ();
running_time = 0f;
currentTarget = null;
}
}
Your answer
Follow this Question
Related Questions
Fall collision force with help of raycast 0 Answers
Raycasting to detect which side of collider is hit 2 Answers
Detect RaycastHit on Character Controller 2 Answers
Raycast Not Drawing In Target Direction 0 Answers
Raycast bullet collision problem 1 Answer