- Home /
Aiming with function "Update" not working
Hello every one, i have been working on a new weapons script that uses real bullets instead of raycasts, i have an issue, the code in my function Update(); isn't working like it used to. here is my gun script so far. ( i have self taught everything i know in javascript, and this is from a base of the FPS tutorial weapon script.)
 // My Gun Combination
 // aiming
 var CanAim : boolean = true;
 var Aiming : boolean = false;
 
 // Charactor Controller
 var controller : CharacterController;
 
 // Transforms
 var muzzlePoint : Transform;
 var muzzleFlash : Renderer;
 var Gun : Transform;
 var sEjector: Transform;
 
 // Game Objects
 var bullet : GameObject;
 var MuzzleSmoke : GameObject;
 var Shell : Rigidbody;
 
 // sound
 var ShootSound : AudioClip;
 // Genral Variables
 var range = 100.0;
 var fireRate = 0.05;
 var force = 10.0;
 var damage = 5.0;
 var bulletsPerClip = 40;
 var clips = 20;
 var reloadTime = 0.5;
 var isFiring : boolean = false;
 var maxPenetration : float = 3;
 var impactForce : int = 50;
 var bulletSpeed : int = 200;
 var shotCount : int = 1;
 
 // Spread / Accuracy
 var baseSpread : float = 1.0;
 var maxSpread : float = 4;
 var spreadPerSecond :float = 0.2;
 var spread : float = 0.0;
 var decreaseSpreadPerSec : float = 0.5;
 var baseSpread_Aim : float = 1.0;
 var maxSpread_Aim : float = 4;
 var Aim_decreaseSpreadPerSec : float = 1;
 var Aim_spreadPerSecond : float = 0.1;
 
 // Private vars
 private var bulletsLeft : int = 0;
 private var nextFireTime = 0.0;
 private var m_LastFrameShot = -1;
 private var bulletInfo : float[];
 
 function Start () {
     // make shure that the muzzleflash isnt on when it is selected xD
     muzzleFlash.enabled = false;
     // start out with a full clip;
     bulletsLeft = bulletsPerClip;
     // make the bullet array to what we want
     bulletInfo = new float[6];
     // more aiming
      Aiming = false;
      //PlayStepAnim(); ------------- i haven't worked this out yet
 }
 function PlayStepAnim () {
     var i : int = 0; 
     while (i <= 10) {
         if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
             //Gun.animation.wrapMode = WrapMode.Loop;
             Gun.animation.Play("Walk");
             i++;
         }
         else {
             yield;
         }
     }
 }
 function Aim () {
     if (Input.GetButtonDown("Fire1"))
         {
             isFiring = true; // fire is down, gun is firing
         }
         if (Input.GetButtonUp("Fire1"))
         {
             isFiring = false; // if fire is up... gun is not firing
         }
         if (isFiring) // if the gun is firing
         {
             spread += Aim_spreadPerSecond; // gun is less accurate with the trigger held down
         }
         else
         {
             spread -= Aim_decreaseSpreadPerSec; // gun regains accuracy when trigger is released
         }
         //=====
 }
 function Hip() {
          if (Input.GetButtonDown("Fire1"))
         {
             isFiring = true; // fire is down, gun is firing
         }
         if (Input.GetButtonUp("Fire1"))
         {
             isFiring = false; // if fire is up... gun is not firing
         }
         if (isFiring) // if the gun is firing
         {
             spread += spreadPerSecond; // gun is less accurate with the trigger held down
         }
         else
         {
             spread -= decreaseSpreadPerSec; // gun regains accuracy when trigger is released
         }
         //===========================================================================================
 }
 function Update () {
     // aiming
      if (Input.GetButtonDown("Fire2"))
         { 
             Aiming = true;
             Debug.Log("Weapon is aiming ");
         }
      if (Input.GetButtonUp("Fire2"))
      {
          Aiming = false;
          Debug.Log("Weapon not is aiming ");
      }    
 }
 function LateUpdate() {
     if (muzzleFlash) {
         // We shot this frame, enable the muzzle flash
         if (m_LastFrameShot == Time.frameCount) {
             muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
             muzzleFlash.enabled = true;
 
             if (audio) {
                 if (!audio.isPlaying)
                     audio.Play();
                 audio.loop = true;
             }
         } else {
         // We didn't, disable the muzzle flash
             muzzleFlash.enabled = false;
             enabled = false;
         }
     }
     // aiming
          if (Aiming == true)
         {
             Aiming = true;
             Aim();
             Debug.Log("Weapon is aiming ");
         }
      if (!Aiming)
      {
          Hip();
          Debug.Log("Weapon not is aiming ");
      }    
     // More Spread Stuff
     if ( Aiming == false ){    
         if (spread >= maxSpread)
             {
                 spread = maxSpread;  //if current spread is greater then max... set to max
             }
         else
         {
             if (spread <= baseSpread)
             {
                 spread = baseSpread; //if current spread is less then base, set to base
             }
         }
     }
     // aiming spread stuff
     if ( Aiming == true ){    
         if (spread >= maxSpread_Aim)
             {
                 spread = maxSpread_Aim;  //if current spread is greater then max... set to max of aiming
             }
         else
         {
             if (spread <= baseSpread_Aim)
             {
                 spread = baseSpread_Aim; //if current spread is less then base, set to base of aiming
             }
         }
     }
 }
 
 function Fire () {
     Update();
     
     if (bulletsLeft == 0)
         //return;
         Reload();
     
     // If there is more than one bullet between the last and this frame
     // Reset the nextFireTime
     if (Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;
     
     // Keep firing until we used up the fire time
     while( nextFireTime < Time.time && bulletsLeft != 0) {
         
             FireOneShot();
             //EmitShell(); dont ask me why this doesnt work, will fix it later
             
         nextFireTime += fireRate;
     }
 }
 
 function FireOneShot () {
     // sound
     var ShotsFired : int = 0;
     var newSmoke : GameObject = Instantiate (MuzzleSmoke, muzzlePoint.transform.position, muzzlePoint.transform.rotation);
     audio.PlayOneShot(ShootSound);
     // Set up bullet info
     bulletInfo[0] = damage;
     bulletInfo[1] = impactForce;
     bulletInfo[2] = maxPenetration;
     bulletInfo[3] = maxSpread;
     bulletInfo[4] = spread;
     bulletInfo[5] = bulletSpeed;
     // used for shotguns
     while(ShotsFired < shotCount ){        
         var newBullet : GameObject = Instantiate (bullet, muzzlePoint.transform.position, muzzlePoint.transform.rotation);
         newBullet.SendMessageUpwards("SetUp", bulletInfo);
         ShotsFired++;
     }
     //Animaioton
     Gun.animation["Test_Shoot"].speed = 3.0;
     Gun.animation.Play("Test_Shoot" , PlayMode.StopAll);
 // Plays the walk animation - stops all other animations
 
     bulletsLeft--;
 
     // Register that we shot this frame,
     // so that the LateUpdate function enabled the muzzleflash renderer for one frame
     m_LastFrameShot = Time.frameCount;
     enabled = true;
     
     // Reload gun in reload Time        
     if (bulletsLeft == 0)
         Reload();            
 }
 
 function Reload () {
 
     // Wait for reload time first - then add more bullets!
     yield WaitForSeconds(reloadTime);
 
     // We have a clip left reload
     if (clips > 0) {
         clips--;
         bulletsLeft = bulletsPerClip;
     }
 }
 
 function GetBulletsLeft () {
     return bulletsLeft;
 }
 function EmitShell (){
     // Shell
     var newShell : Rigidbody = Instantiate (Shell, sEjector.transform.position,sEjector.transform.rotation);
     newShell.velocity = transform.TransformDirection(Vector3 (2, 0,0));
     
 }
Sry its kind of long, but what happens is when the update function is called, i want 'Aiming' to be true when "Fire2" button is pressed, and 'Aiming' not true when "Fire2" isnt pressed. but this doesn't happen, i have moved lots of code around trying to figure out the problem. if anyone knows what is happening, please help me. BTW i am useing a bullet script from Jeremy Clark, You will need the bullet script to make the gun script work, but i wont put it in this page, but you can download it here.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                