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 ToxicNova · Apr 12, 2015 at 08:15 PM · javascriptraycastgununity 4.6ammo

How to send a raycast to an object to give me ammo?

Hello, so I have a ammo pack that I has a script which holds a variable. I want to be able to detect the ammo packs from a certain distance, send a message to it telling it to give ammo to my gun. I have some scripts bellow but they didn't work (note that these aren't to full scripts, just the parts I'm trying to fix).

 //-script attached to my gun-under the update function-
 
 if ((Input.GetKeyDown(KeyCode.E)))
      {
          var hit : RaycastHit;
         if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
         {
             Distance = hit.distance;
             }
         if (Distance < MaxDistance)
         {
             hit.transform.SendMessage("PickUpAmmo", TAmmo, SendMessageOptions.DontRequireReceiver);
         }
     }


 //the ammo pack
 
 #pragma strict
 
  var TRecive : int = 0;
  var Ammo_PickUp : int = 40;
  var Gun : GameObject;
  
 function Update () 
 {
     if (TRecive == 1)
     {
         Gun.SendMessage("PickAmmo", Ammo_PickUp, SendMessageOptions.DontRequireReceiver);
     }
 }
 function PickUpAmmo (TAmmo : int)
 {
     TRecive = (TRecive + TAmmo);
 }

 //Finally, the last part of my gun script that receives the PickUpAmmo (or whatever its called) message 
 
 function PickAmmo (Ammo_PickUp : int)
 {
     ammo = (ammo + 40);
 }

Thanks in advance! (Using unity 4.6 and unityscript)

Comment
Add comment · Show 3
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
avatar image Matheuz · Apr 12, 2015 at 09:24 PM 0
Share

Hi ToxicNova. Could you post the full gun script?

avatar image ToxicNova · Apr 13, 2015 at 12:33 PM 0
Share

Yea sure, but I'm typing this on my phone; won't be able to send the full script until 11:30 or around that time.

avatar image ToxicNova · Apr 13, 2015 at 05:00 PM 0
Share

Here is the gun script:

  #pragma strict
  
  import UnityEngine.UI;
    
  var bullet : GameObject;
  private var bulletsPerSecond = 20.0;
  private var shooting = false;
  private var Allammo : int = 320;
  var ammo : int = 40;
  var sound: AudioClip; 
  var Gun : GameObject;
  var textStyle : GUIStyle;
  var Reloadammo : int = 0;
  var Distance : float;
  var $$anonymous$$axDistance : int = 2;
  var TAmmo : int = 1;
  var Remainder : int = 0;
  
  function Start()
  {  
      InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
  }
  function Shoot()
  {
      if (!shooting || ammo < 1) return;
      var go = Instantiate(bullet, transform.position, transform.rotation);
      go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
      ammo = (ammo - 1);
      audio.PlayOneShot(sound);
  }
  function Update()
  {
      if ((Allammo > 0) || ((ammo <= 40) && (ammo > 0) && (Allammo == 0)))
       {
           if (ammo > 0)
           {
               if (Input.GetButtonDown("Fire1"))
               {
                   shooting = true;
               }
               if (Input.GetButtonUp("Fire1"))
               {
                   shooting = false;
               }
           }
           else
           {
               shooting = false;
               if (40 <= Allammo)
               {
                   if (320 >= Allammo)
                   {
                       Allammo = (Allammo - 40);
                       ammo = 40;
                   }
         
               }
               if (40 > Allammo)
               {
                   if (ammo != 40) 
                   {
                       ammo = Allammo;
                       Allammo = 0;
                   }
               }
           }
       }
       else shooting = false;
       
      if ((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) && (Allammo > 0) && (ammo < 40))
      {
          Reloadammo = (40 - ammo);
          if (Allammo >= Reloadammo)
          {
               ammo = 40;
               Allammo = (Allammo - Reloadammo);
           }
      } 
       if ((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)))
       {
           var hit : RaycastHit;
          if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
          {
              Distance = hit.distance;
              }
          if (Distance < $$anonymous$$axDistance)
          {
              hit.transform.Send$$anonymous$$essage("PickUpAmmo", TAmmo, Send$$anonymous$$essageOptions.DontRequireReceiver);
          }
      }   
  }
  function PickAmmo (Ammo_PickUp : int)
  {
      ammo = (ammo + 40);
  }
  

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

19 People are following this question.

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

Related Questions

Ammunition scripts 4 Answers

musket gun script is not working 3 Answers

Gun script using Physics.Raycast not working? 2 Answers

gun ammo script 2 Answers

Projectile Firing/Gun script 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