- Home /
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
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.
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);
}
Your answer
Follow this Question
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