Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by XIV-Studios · Aug 25, 2011 at 07:52 PM · updatefunctiongunaimingweapons

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Question... Use the switch to update or function 0 Answers

Why doesen't the function get called the second itme? 1 Answer

Gun aim off center 0 Answers

checking multiple textures at once using functio update 1 Answer

Update() doesn't update so well 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges