- Home /
lock on enemy 3d spaceshooter
Hi,
I use a ps3 controller in my spaceshooter and to aim I use the right analog stick. When my aimcursor is near an enemy, I want the cursor to snap to the enemy. To do this I use a Spherecast. If the spherecast hits an object which is in my array of enemies, I want the cursor to move to the right position (with camera.worldtoscreenpoint()) When I check if the object is in my array however, it never enters the if statement. Here's my code:
if(Physics.SphereCast(oRay, 5, out oHit, 500))
{
Debug.DrawLine(oRay.origin, oHit.point, Color.yellow);
print(oHit.collider.gameObject);
target = oHit.point;
if(enemies.Contains(oHit.collider.gameObject))
{
print("enemy found");
}
}
else
{
//stuff
}
The print shows which object the spherecast has hit. In my case it prints 'Enemy'. In my Array I have an object 'Enemy'. So it should also print "enemy found", right? nope. Could anyone tell me what the problem is?
If you need more info to answer my question, just ask.
Answer by AeonIxion · Oct 09, 2012 at 04:07 PM
Ok so I guess I was using the Contains method wrong. I changed it to:
if(Physics.SphereCast(oRay, 5, out oHit, 500))
{
Debug.DrawLine(oRay.origin, oHit.point, Color.yellow);
print(oHit.collider.gameObject);
target = oHit.point;
foreach(GameObject enemy in enemies)
{
if(oHit.collider.name.Equals(enemy.name)
{
print("enemy found");
}
}
}
else
{
//stuff
}
Your answer
Follow this Question
Related Questions
AudioClip.SetData not working 2 Answers
Array copy 2 Answers
Wrong object always Instantiated 1 Answer
Read different game objects into array at same time? 2 Answers
How to make an array list set to false gradually instead of instantly? 2 Answers