Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zero84085 · Sep 08, 2012 at 01:15 PM · raycasttransformvector3shootingbullets

RayCast Shooting And Errors

hello guys my name is john and i working in my script, things going good but i have a small problem, i want to collect bullets from boxes ... the first thing i do was to make a transfrom variable and with vector3 when i wan something like player position < 3 in the transfrom i can press the "e" key and collect the bullets (variable + bullets) - ( BulletsLeft + 15); but i am not sure why is not working.... here is my script, i have mark with // the problem start - end. please help, if you find something usefull for my scipt add comment.

 var Range : float = 1000;
 var Force : float = 1000;
 
 var Clips : int = 20;
 var BulletPerClip : int = 60;
 var BulletsLeft : int = 0;
 var ReloadTime : float = 3.3;
 
 var Damage : int = 10;
 var BurstingDamage : int;
 
 var isSimpleMode : boolean;
 
 var PistolAmmoBox : Transform;
 var RiffleAmmoBox : Transform;
 var ShotgunAmmoBox : Transform;
 var SniperAmmoBox : Transform;
 var HealthKit : Transform;
 var Grenade : Transform;
 
 var mySkin : GUISkin;
 
 var MoveSpeed: float = 1;
 var ShootTimer : float = 0;
 var ShootCooler : float = 0.5;
 
 var ShootAudio : AudioClip;
 var EmptyFullWeaponAudio : AudioClip;
 var SwitchModeSound : AudioClip;
 var ReloadAudio : AudioClip;
 
 var muzzleFlash : ParticleEmitter;
 var hitParticles : ParticleEmitter;
 var BloodEffect : ParticleEmitter;
 var BloodEffectMain: ParticleEmitter;
 var Metal : ParticleEmitter;    
 
 var muzzleFlashTimer : float = 0;
 var KeyCooler : float = 0.5;
 var KeyTimer : float = 0;
 var muzzleFlashCooler : float = 0.5;
 
 var Light1 : Light;
 var Light2 : Light;
 var Light3 : Light;
 
 var crosshairFirstModeHorizontal : Texture2D;
 var crosshairFirstModeVertical : Texture2D;
 var crosshairSecondMode : Texture2D;
 private var adjustMaxCroshairSize : float = 6.0;
 
 var canSwicthMode : boolean = true;
 var bursting : boolean = false;
 
 var maxDist : float = 1000000000;
 var woodHitWall : GameObject;
 var concreteHitWall : GameObject;
 var floatInFrontOfWall : float = 0.00001;
 
 var gunGUI : GUIStyle;
 var rect1: float;
 var rect2: float;
 var rect3: float;
 var rect4: float;
 
 var IdleAnim : GameObject;
 var ShootAnim : GameObject;
 var ReloadAnim  : GameObject;
 var Reload2Anim : GameObject;
 var EmptyAnim : GameObject;
 var RunAnim : GameObject;
 var ShiftRunAnim : GameObject;
         
 function Start(){
 BulletsLeft = BulletPerClip;
 
 isSimpleMode = true;
 
 DefaultPos = transform.localPosition;
 
 muzzleFlash.emit = false;
 hitParticles.emit = false;
 BloodEffect.emit = false;
 Metal.emit = false;
 BloodEffectMain.emit = false;
 
 Light1.enabled = false;
 Light2.enabled = false;
 Light3.enabled = false;
 
 }
 function OnGUI (){   
      GUI.Label(new Rect(Screen.width/rect1,Screen.height/rect2, Screen.width/rect3,Screen.height/rect4 ),""+BulletsLeft+"/"+Clips+"",gunGUI);
      }
  function Update(){
    if( KeyTimer > 0){
       KeyTimer -= Time.deltaTime;
 }
 if( KeyTimer < 0){
      KeyTimer= 0;
 }
 if( muzzleFlashTimer > 0){
      muzzleFlashTimer -= Time.deltaTime;
   MuzzleFlash();
 }
 if( muzzleFlashTimer < 0){
      muzzleFlashTimer = 0;
 }
 if( ShootTimer > 0){
              ShootTimer -= Time.deltaTime;
    }
    if(ShootTimer < 0){ 
                    ShootTimer = 0;
                   }
              if( KeyTimer == 0){
             if(Input.GetMouseButtonDown(0) && BulletsLeft ){    
             if( ShootTimer  == 0){
             PlayShootAudio();
                 RayShoot();
                 ShootTimer  = ShootCooler;
                 KeyTimer = KeyCooler;
                 }
                     if( muzzleFlashTimer == 0){
                   muzzleFlashTimer = muzzleFlashCooler;   
                         MuzzleFlash ();    
 }                
 }
 }
 }
 function MuzzleFlash (){
     if( muzzleFlashTimer > 0){
          muzzleFlash.emit= false;
            
            Light1.enabled = false;
            Light2.enabled = false;
            Light3.enabled = false;
 }
 if( muzzleFlashTimer == muzzleFlashCooler){
       muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 , Vector3.forward);
          muzzleFlash.emit = true;
            
             Light1.enabled = true;
            Light2.enabled = true;
            Light3.enabled = true;
 //////////////START PROBLEM///////////////
 }
 if(Vector3.Distance(PistolAmmoBox.position, transform.position) < 3){
       BulletsLeft  * 1.5;
 }
 }
 //////////////END PROBLEM ///////////
 function RayShoot (){
 var hit : RaycastHit;
 if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
 {
   if (concreteHitWall && hit.transform.tag == "Concrete")
   Instantiate(concreteHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)); 
 }
 
 var direction : Vector3  = transform.TransformDirection(Vector3.forward);
 Debug.DrawRay(transform.position , direction * Range , Color.red);
 if(Physics.Raycast(transform.position , direction , hit, Range)){
 var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
 if(hit.rigidbody){
 if(hitParticles){
 hitParticles.transform.position = hit.point;
 hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
 hitParticles.Emit(); 
 hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
 hit.collider.SendMessageUpwards("ApplyDamage" , Damage , SendMessageOptions.DontRequireReceiver);
 }
 if(hit.collider.gameObject.CompareTag("Enemy")){
 if( BloodEffect ){
 if( BloodEffectMain){
 BloodEffect.transform.position = hit.point;
 BloodEffect.transform.position = hit.point;
 BloodEffect.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
 BloodEffectMain.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
 BloodEffect.Emit();
 BloodEffectMain.Emit();
 }
 }
 }
 }                
 }
                 BulletsLeft --;
                 if(BulletsLeft < 0){
                 BulletsLeft = 0;
                 }
                 if( BulletsLeft == 0 ){
                 SwitchMode();
                 Reload();
                 }
                 }
                 
 function SwitchMode(){
 if(Input.GetButtonDown("SwitchMode") && isSimpleMode == true){
 bursting = true;
 if (bursting == true){
 Damage = 25;
 isSimpleMode = false;
 }
 else if(Input.GetButtonDown("SwitchMode") && isSimpleMode == false){
 bursting = false;
 }
 if(bursting == false){
 Damage = 50;
 }
 isSimpleMode = true;
 }
 }
 
 function Reload(){
         PlayReloadAudio();
         yield WaitForSeconds(0.2);
      yield WaitForSeconds( ReloadTime);
              if(Clips > 0){
                 BulletsLeft = BulletPerClip; 
                            Clips -= 1;           
 }
 }
 function PlayShootAudio(){
         audio.PlayOneShot( ShootAudio);
 }
 function PlayReloadAudio(){
   audio.PlayOneShot( ReloadAudio);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 08, 2012 at 02:46 PM

Why is the problematic code inside the MuzzleFlash function? Is this an intended feature? Anyway, this code would never do what you expect! You should do the job inside Update, and with a totally different code:

function Update(){
  if (Input.GetKeyDown("e") && Vector3.Distance(transform.position, PistolAmmoBox.position) < 3){
    BulletsLeft += 15; // get 15 bullets from the ammo box
  }
  ... // other Update code
But you must clean up your code: it's currently reloading ammo clips, but you want to get the bullets directly. You can set Clips to zero at Start(), what will let the BulletsLeft variable control the ammo alone, or change the code a little to get a full ammo clip instead of a handful of bullets, like this:

function Update(){
  if (Input.GetKeyDown("e") && Vector3.Distance(transform.position, PistolAmmoBox.position) < 3){
    Clips++; // get a full clip
  }
  ... // rest of Update code
Comment
Add comment · Share
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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move the bullet from the BulletEmitter in the direction player is facing 3D. 2 Answers

Raycast between TransformDirection and Vector3.Forward? 1 Answer

Object Does not Destroyed While Shooting 1 Answer

Locking gameobject to rotating floor 0 Answers

Unity Rotate Raycast on Quaternion 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