• 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 /
  • Help Room /
avatar image
0
Question by TheAllan · Oct 28, 2015 at 01:16 PM · fpsshootingweaponhidereload

Help me pleaase!!! Problems with shooting script and reload

Hello I have this script that I got and implement some things in it, the problem is when reload the gun bullets station eg in texture 30/60, then 30/30, then 30/0, there when I spend these 30 rounds, the to recharge there ficaa 30 / -30, please help me, help me very much!

var bulletTex : GameObject[];

var power : int = 10;

var fireRate : float = 0.5;

private var nextFire : float = 0.0;

var gunShot : AudioClip;

var bullets : int = 5;

var reloadSound: AudioClip; //reload soundClip

var Balas = 30; //amount of coconuts per "clip"

var totalBalas = 60; //total amount of coconuts we have available

var reloadTime = 3; //time to reload

var reloadAmount = 10;

var ammo : GUIText; //GUI text for our coconut/ammo count

function Update () {

Reload ();

Fire ();

}

function Fire () {

if(Balas > 0) { //if we have at least 1 coconut then we can fire

if(Input.GetButton("Fire1") && Time.time > nextFire) {

nextFire = Time.time + fireRate;

AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);

ForceFire();

}

}

}

function ForceFire () {

var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction

var hit : RaycastHit;

Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters

if(Input.GetButton("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something bullets--;

Instantiate(bulletTex[Random.Range(0,1)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust

if(hit.collider.gameObject.tag == "Inimigo"){ //hit.rigidbody.AddForceAtPosition( direction * 100 , hit.point); //hit.collider.gameObject.animation.Play("die"); hit.collider.gameObject.SendMessage("ApplyDamage", 25);

} // the position and rotation of textures to match the object being hit

if (hit.rigidbody !=null)

hit.rigidbody.AddForceAtPosition(fwd * power, hit.point); //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click

}

Balas -=1;

} function Reload () {

if(Balas < 1) { //if we have less than 1 coconut then we can reload

if(Input.GetKeyDown("r")) {

AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1); //plays reload soundclip

yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding coconuts

Balas += reloadAmount; //adds 3 coconuts to our "clip"

totalBalas -= reloadAmount; //subtracts 3 coconuts from our totalCoconuts amount

if (totalBalas < 0) { totalBalas--;

}

}

}

}

function OnGUI () {

ammo.text = "Balas: " + Balas + "/" + totalBalas.ToString();

}

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get bullet hole to wrap around corners of objects? 0 Answers

How to make gun shoot in the crosshair & automatic fire (Javascript) 1 Answer

Camera Recoil Script Not working Correctly 0 Answers

Basic FPS help! 2 Answers

FPS Gun Hold 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges