I’m still rusty with my C# arrays, and have run into an implementation brick wall.
What I’m Trying To Do:
Fire a raycast after pressing a button. Once that raycast hits a collider that has been tagged “Enemy”, place a lockIcon game object over the hit object.
The Problem:
Here’s the original code block I cooked up:
void FireOneShot ()
{
// Bit shift the index of the layer (8: Player) to get a bit mask.
LayerMask layerMask = 1 << 8;
// Inverse bitmask so that raycast ignores the Player's layer.
layerMask = ~layerMask;
// Find the game camera so that the raycast knows to fire directly at the center of the screen.
GameObject camera = GameObject.FindWithTag("MainCamera");
Vector3 direction= transform.TransformDirection(Vector3.forward);
RaycastHit hit = new RaycastHit();
GameObject enemy = GameObject.FindGameObjectWithTag("Enemy");
// Did we hit anything?
if (Physics.Raycast (camera.transform.position, direction, out hit, range, layerMask)) // << added layerMask so that raycast completely ignores Player's collision.
{
Debug.Log("Raycast");
if (hit.collider == enemy)
{
locked = true;
Debug.Log("Hit Locked");
lockIcon.renderer.enabled = true;
Vector3 icon = lockIcon.transform.position;
Vector3 enemyPos = enemy.transform.position;
icon.y = (enemyPos.y+1);
icon.x = (enemyPos.x);
icon.z = (enemyPos.z);
}
}
}
Of course, with over 30 objects tagged with “Enemy” in the level, the above code just assigns the ‘enemy’ variable to 1 of these objects; it won’t recognise hit.collider if the raycast hits any other tagged enemies.
I know we can use arrays in C#, but the Unity Scripting Reference for Arrays unhelpfully only gives JavaScript examples.
but am now stuck at testing to see if any of the enemy variables in the GameObject[ ] array have been hit; if (hit.collider == enemy) no longer works with a GameObject[ ] array.
Any help and/or pointers for testing against colliders of objects in an array in C# would be most gratefully welcomed!
Q: Why did the programmer quit his job?
A: Because he didn’t get arrays.
Thanks for the elegant solution Myx! I still have to sort out how to keep updating the new lockIcon with the targeted enemy’s position, but the raycast detection now works.
A programmer is strangely enough walking around on a beach when all of a sudden he finds a lamp. Not the new fancy electric kind of lamp, oh no, but one of the old arabic ones. Being a fan of mythological stories he rubs the lamp, and to his great suprise a genie appears!
“Greetings mortal, I have come to grant you a single wish!”
The programmer pulls out a map, points at the Middle East and says: “I wish there was peace here.”
The genie looks a bit troubled and answers,
“Hmm… They’ve been fighting there for an awful long time, fixing peace there would be nearly impossible. Is there maybe something else you’d like?”
After a few minutes of consideration the programmer proclaims “I’m a programmer and the programs I make have alot of users. I wish all my users were content and satisfied with my software and only asked for sensible changes.”
The genie looks at the programmer for a few moments. “Uhm, could I see that map again?”
Right then, your problem can be solved with a reference to the target’s transform.
Something along these lines:
Sir Myx, you truly deserve a medal for going above and beyond the line of duty!
The intent of my original code was to have the targeting icon floating above the enemy’s head (hence the convoluted code just to get the icon’s y position as the target’s y transform + 1). However, using your code, the target icon actually looks better once I blow it up to 3x its original size; as the icon is now centered on the target’s root transform, the larger icon surrounds the model and actually makes a better ‘locked-on’ indicator!