- Home /
im having trouble with raycast shooting script
well its in the description. i am getting some errors and i cant figure out what is going on. here is the script, and thanks in advance
var Bullets : int; var CanShoot : boolean; var TheDamage = 10; var reloadTime = 5; var totalAmmo = 60; var reloadAmount = 30;
function Update ()
if (totalAmmo < 1) { CanShoot == false; }
if (totalAmmo > 0) { CanShoot == true; }
{ if (CanShoot == false) reload();
}
if (CanShoot == true) { Shoot(); }
function Shoot () {
var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0)) { if (Physics.Raycast (ray, hit, 100)) { hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver); } } }
function reload () {
if(Input.GetKeyDown("r")) {
yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount
totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload
}
}
the result of a physics raycast is not boolean, so the "if" worries me.
do you not need to define what "hit" is?
I am c# only so I may be wrong.
The static function raycast does return a boolean :
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
static function Raycast(origin: Vector3, direction: Vector3, distance: float = $$anonymous$$athf.Infinity, layer$$anonymous$$ask: int = DefaultRaycastLayers): bool;
However, you are correct about the OP needing to declare the RaycastHit hit variable.
Your answer
Follow this Question
Related Questions
Problem with hitInfo in Raycast. 2 Answers
Raycasting in script suddenly stopped working 1 Answer
Error on Line 28: expecting EOF found "}" 2 Answers
Help with a simple following AI 1 Answer
need help converting gun script from c# to javascript 1 Answer