- Home /
Help on a pick up/reload script
Hi,
I'm trying to make a game were the player needs to find a bullet and a gun to shoot an enemy. So I have tried to write a reload script which is attached to the Player and goes like this:
var canReload = false;
var carryingGun = false;
var carryingBullet = false;
var collisionGun : Collider;
var collisionBullet : Collider;
var reloadTime = 3;
var bullets = 1;
var pickupGunSound : AudioClip;
var pickupBulletSound : AudioClip;
function OnCollisionEnter(collision : Collision)
{
if (collisionGun.gameObject.tag == "gun"); // collide with the Gun...
carryingGun = true; // ...to pick it up
audio.PlayOneShot(pickupGunSound);
Debug.Log( "YOU HAVE THE GUN" ); // "You have the gun. Find the bullet"
if (collisionBullet.gameObject.tag == "bullet") // collide with the Bullet...
carryingBullet = true; // ...to pick it up
audio.PlayOneShot(pickupBulletSound);
Debug.Log( "YOU HAVE THE BULLET" ); // "You have the bullet. Find the gun"
}
function Reload ()
{
if (carryingGun == true)
canReload = true;
Debug.Log( "YOU CAN RELOAD" );
if (carryingBullet == true)
canReload = true; // ...you can reload
Debug.Log( "YOU CAN RELOAD" ); // "You have the bullet and the gun. Reload now (press R)!"
if(canReload == true)
if (Input.GetKeyDown("R"))
{
yield WaitForSeconds (reloadTime); // wait 3 seconds
bullets = 1; // there is one bullet in the Gun
Debug.Log( "THE GUN IS LOADED" ); // "The Gun is Loaded. Shoot the enemy !"
}
}
So the console sends no errors back and I get the debug.log for carryingGun=true and carryingBullet=true, but I can"t get the canReload=true and reload state.
Please, can you correct my code or help me see what I'm missing here. Thanks.
Answer by sparkzbarca · Nov 16, 2012 at 01:00 AM
i'm not certain but dont you need to bracket that since its more than one line?
You shouldnt use 2 if statements like that anyways you should AND them together.
if(CarryingGun && CarryingBullet) //the == true is implicit you dont need to type it
{
CanReload = true;
}
if(CanReload && Input.GetKeyDown("R"))
{
yield WaitForSeconds(reloadTime);
bullets = 1;
}
Answer by Kaha · Nov 16, 2012 at 10:29 AM
Hi,
if(CarryingGun && CarryingBullet) //the == true is implicit you dont need to type it
{
CanReload = true;
}
is working great, thanks ! But for
if(CanReload && Input.GetKeyDown("R"))
{
yield WaitForSeconds(reloadTime);
bullets = 1;
}
I get errors in the console, so I'll stay with the double if statements since it's working here.
Anyway, thanks for sharing your knowledge !
Answer by PAHeartBeat · Nov 17, 2012 at 08:45 AM
HI Kaha
Please check you components which are using in your player gameObject if you are using Character controller then you can't access directly OnCollisionEnter Event you need to use "`OnControllerColliderHit`" Event
Hi Ranpariya Ankur, I will check if this is the reason why my Gun and Bullet collisions are glitchy.
Your answer
Follow this Question
Related Questions
Ammo Script 1 Answer
How can I shooting script c#? 0 Answers
c# weapon shooting script using raycasting help 1 Answer
reload gun with bullets 2 Answers