Hard time hitting objects with raycast

I’m trying to do something very basic. I’ve got AI’ed monsters coming at the player. The player is to point and shoot (monsters get killed and removed by one hit).

Here comes the problems:
I’m using a character controller on the monsters (so it has a capsule collider). I have a hard time actually hitting the monsters, even though the collider is fairly large. If I multiply the skin width value it becomes a lot easier to hit the enemies. This, however, really bugs up their collision and makes them run above the mazed walls.

Second problem is that the mouse isn’t centered and moving according to the character crosshair. So I have to “shoot” the enemies by clicking them with the actual mouse cursor. I’d like to fix that.

Scripts:

function Update () {
var hit : RaycastHit;
var geneStealer : GameObject;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// (Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y,0)
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (ray, hit, 50)  Input.GetButtonDown ("Fire1")) {
var hitEnemy : GameObject = hit.collider.gameObject;

 if (hitEnemy.tag == "genestealer"){
 Destroy(hitEnemy);
// Destroy(hit.collider.gameObject);
}

}
}

Edit:
I want to control the time aspect of some stuff, so I want a value to be added every second or so (I just want to control the value adding with time)

soo…
var myVar;
myVar += Time.deltaTime * 1;
Debug.Log(myVar); // To check if value gets added every second
^ That’s what my intentions were, but I dont know how to set it up properly.

Regarding your crosshair problem, I’m not sure what you mean. But if the crosshair object is not centered at the mouse pointer, might have to do with the pivot point of that object.

An easy way to fix it is to make sure the pivot point of the crosshair to be on the center. Then, you can just assign the position of that object to the position of the mouse pointer.

If I’m not getting your problem, I’m sorry.

Regarding the collision problem, try to remove the “Input.GetButtonDown(“Fire1”)” condition on the if, and print (Debug.Log()) out the result of the Raycast each frame. At least to be sure if it’s a raycast problem, a speed problem (enemies/player moving too fast), an input problem.

Thanks. Centered the cursor now and ran the debugging. Seems like the problem is the raycast alright. Guess I’ve gotta try debugging more.

The problem was rather simple (isn’t it always?).
The camera was actually inside of the character-mesh, which all makes sense now.

On an unrelated note, I have another question.
I want to control the time aspect of some stuff, so I want a value to be added every second or so (I just want to control the value adding with time)

soo…
var myVar;
myVar += Time.deltaTime * 1;
Debug.Log(myVar); // To check if value gets added every second
^ That’s what my intentions were, but I dont know how to set it up properly.

It start a bit more technical but will end up a really elegant solution if you use coroutines.

function AddToValue()
{
   while(true)
   {
      myValue++;
      yield WaitForSeconds (5);
   }
}

Calling that once on start will create a permanent loop that will add 1 to myValue every 5 seconds. If you adjust the while loop you can exit the loop (turn off the adding) by just setting that to false.

I’m not used to Java so that may be bad syntax, but check out Unity - Scripting API: Coroutine for some more information.

Thanks, will try that. And I dont mind more technical at all, love challenges! :slight_smile:

Just wanted to say it worked great for me, thanks!

I’m slowly but surely getting better at scripting so I’ve started figuring out some things myself, but not everything just yet.