- Home /
Shooting system not working
Hey guys how's it going? I am trying to make a script for shooting using RayCast I wrote it so that when the RayCast hits an object tagged "enemy" and the player presses fire1 enemy will take damage but no damage is done:
 var bullet : Rigidbody;
 var speed : float = 20;
 
 function Start () {
 attackTimer = 0;
 }
 
 function Update () {
 var hit : RaycastHit;
 if(Physics.Raycast(transform.position, transform.forward, hit, 3)){
 if(hit.collider.gameObject.tag == "Enemy"){
 if(Input.GetButtonUp("Fire1")){
 Shoot();
 }
 }
 }
 }
 
 
 private function Shoot (){
 var newBullet : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
 newBullet.velocity = transform.forward * speed;
 var eh : EnemyHealth = GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }
Any ideas/fps tutorials?
I'm not sure what you want to do here. GetButtonUp() will only return true for a single frame when the button is lifted. GetButtonDown() might be what you are looking for. Put a debug statement inside the raycast to make sure the raycast is working and hitting the enemy (ins$$anonymous$$d of some other collider).
 Debug.Log("Name = "+hit.collider.name); 
the script is attach to the pistol which is attached to the player I'm trying to cast a ray from the gun, checks if the ray hits the enemy (hit.gameObject.tag == "Enemy) then if the player hits fire1 the enemy's health drops (eh.AdjustHealth (-5)) I put a Debug.Log function but it didn't work and the console doesn't show errors. Do you think I have a better chance trying the OnCollisionEnter(col : Collision) and place it on the bullet prefab?
Answer by BlackWingsCorp · Feb 27, 2013 at 11:35 PM
Well the ray is casting but only 1 enemy is taking damage here's the script:
 var force : float = 10;
 var range : float = 200;
 var enemies : GameObject[];
 var enemy : GameObject;
 function Start () {
 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 enemy = enemies[0];
 }
 
 function Update () {
 RayShoot();
 }
 
 
 function RayShoot (){
 var hit : RaycastHit;
 var directionRay = transform.TransformDirection(Vector3.forward);
 Debug.DrawRay(transform.position, directionRay * range, Color.blue);
 if(Physics.Raycast(transform.position, directionRay, hit, range)){
 if(hit.collider.tag == "Enemy" && Input.GetButtonUp("Fire1")){
 enemy = enemies
 var eh : EnemyHealth = enemy.GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }
 }
 }
I want enemy to change depending on where the player is aiming. Any idea guys?
Your answer
 
 
             Follow this Question
Related Questions
Enemy death help 1 Answer
Distance on fps 3 Answers
Lens-Flare Muzzleflash 0 Answers
gun scope cam not working 3 Answers
Problem With Weapon Firing Mechanism 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                