shoot script isn't working...

Howdy

I’m in the early stages of a sci-fi fps, and now working on being able to shoot.

My problem is that when i shoot with all my scripts attached, it doesn’t do anything. I don’t get any errors, it just doesn’t do anything. I’ve been trying to figure it out on my own, but i finally decided i need some help…

Here’s my gun script:

var Bullet : GameObject;
var speed : float = 0.4;
var ammo : float = 60;
var particle : ParticleEmitter;
var damage = 2;

function Update () {
  speed = Time.deltaTime;
  var hit : RaycastHit;
  var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.MousePosition.x, Screen.height - Input.MousePosition.y,0));
  if(Input.GetButtonDown("Fire1") && ammo>=0){
    if (Physics.Raycast (ray, hit, 100)) {
      if (Physics.Raycast (ray, hit, 100)) {
        Instantiate(particle, hit.point, Quaternion.LookRotation(hit.normal));
        var otherObj : GameObject = hit.collider.gameObject;
        if (otherObj.tag == "Enemy") {
          otherObj.GetComponent(typeof(EnemyHealth)).Death(damage);
          var projectile:GameObject = Instantiate(Bullet,transform.position,transform.rotation);
          projectile.GetComponentInChildren(Rigidbody).velocity = transform.TransformDirection(Vector3(0,0,speed));
          ammo -= 1;
        }
        if(ammo <=10){
          ammo += speed;
        }
      }
    }
  }
}

and here’s the enemy script:

var Health : float = 10;

function Death ( damage : float) {
  Health -= damage;
  if(Health <= 0 ) {
    animation.play("die");
  }
}

When i select the gun and shoot, i can see not even the ammo var in the inspector changes. any help would be appreciated.

if (Physics.Raycast (ray, hit, 100))

If you can’t see the ammo changes. It can be the if statement has the problem. Maybe try to change that to check if it is returning a true value or try to remove the if statment.

TIP: Please use Code Sample in writing your code for code reading purposes.

Have a good day.

There are several wrong or weird things in this script:

1- You don’t have to modify mousePosition (with low case m) - just pass it directly to ScreenPointToRay (only GUI screen coordinates are upside down);

2- It would be better to move the ray creation inside the if - it’s not wrong, but wastes time since you only need it when shooting;

3- Why did you use 2 consecutive Raycasts? One is enough!

4- If Unity complains that Death isn’t part of Component, use the generic GetComponent version (it returns an EnemyHealth reference):

 otherObj.GetComponent.<EnemyHealth>().Death(damage);

5- You don’t need to instantiate the bullet when shooting with raycast, but if you want to do it, use a suitable speed ( currently you’re using speed = Time.deltaTime, which will make the bullet fly like Matrix projectiles!) And you should access its rigidbody with projectile.rigidbody: unless the rigidbody is attached to a child in your prefab, GetComponentInChildren is a complicated way to get it.

6- You’re decrementing ammo only when an enemy is hit; is it a intended behaviour? If not, move it outside the if.

The fixed version of your script is:

var Bullet : GameObject;
var speed : float = 0.4;
var ammo : float = 60;
var particle : ParticleEmitter;
var damage = 2;

function Update () {
  speed = Time.deltaTime;
  if(Input.GetButtonDown("Fire1") && ammo>=0){
    // only create the raycast when shooting:
    var hit : RaycastHit;
    // pass Input.mousePosition directly to ScreenPointToRay:
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    // why 2 raycasts? one is enough:
    if (Physics.Raycast (ray, hit, 100)) {
      Instantiate(particle, hit.point, Quaternion.LookRotation(hit.normal));
      var otherObj : GameObject = hit.collider.gameObject;
      if (otherObj.tag == "Enemy") {
        otherObj.GetComponent(typeof(EnemyHealth)).Death(damage);
        var projectile:GameObject = Instantiate(Bullet,transform.position,transform.rotation);
        // access the rigidbody directly - by the way, at this speed
        // the bullet it will look like Matrix projectiles
        projectile.rigidbody.velocity = transform.forward * speed;
      }
      // decrement ammo outside the if, or only successful shots will count
      ammo -= 1;
      if(ammo <= 10){
        ammo += speed;
      }
    }
  }
}