- Home /
Ammo Script
Hello, i have my basic shooting script with bullet clone, muzzle flash & bullet texture instantieted and i dont know how to make reload function, heres my script:
var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
var projectile : Rigidbody;
var speed = 100;
var power : int = 10; //provides power to our raycast that can affect our rigidbody components
var muzzelFlash : GameObject;
var fireRate : float = 0.5;
private var nextFire : float = 0.0;
var gunShot : AudioClip;
private var fullAuto = false;
function Start () {
}
function Update () {
Fire ();
}
function Fire () {
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
if (muzzelFlash)
holdMuzzelFlash = Instantiate(muzzelFlash, transform.position, transform.rotation);
if (holdMuzzelFlash)
holdMuzzelFlash.transform.parent = transform;
ForceFire();
}
if (Input.GetKeyDown("w")) {
fullAuto = !fullAuto;
}
if ( fullAuto == true) {
fireRate = 0.5;
}
else {
fireRate = 0.5;
}
}
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.GetButtonUp("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
animation.Play();
Instantiate(bulletTex[Random.Range(0,3)], 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
// 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
}
}
Comment
$$anonymous$$Y GOD YOU AND CypherGames ARE WORLD $$anonymous$$OST S$$anonymous$$ARTEST PERSONS ON WORLD THAN$$anonymous$$S!
Best Answer
Answer by CypherGames · Aug 18, 2013 at 01:16 PM
Hey, erda123! I may have thae answer to your question. Try this: var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
var projectile : Rigidbody;
var speed = 100;
var power : int = 10; //provides power to our raycast that can affect our rigidbody components
var muzzelFlash : GameObject;
var fireRate : float = 0.5;
private var nextFire : float = 0.0;
var clip : int = 30;
var totalBullets : int = 180f;
private var clipSize : int;
var gunShot : AudioClip;
var reloadSpund : AudioClip;
var emptyClickSound : AudioClip;
private var fullAuto = false;
function Start () {
clipSize = clip;
}
function Update () {
if (clip > 0) {
Fire();
}
else {
if (totalBullets > 0) {
Reload();
}
else {
Empty();
}
}
}
function Fire () {
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
if (muzzelFlash)
holdMuzzelFlash = Instantiate(muzzelFlash, transform.position, transform.rotation);
if (holdMuzzelFlash)
holdMuzzelFlash.transform.parent = transform;
ForceFire();
}
if (Input.GetKeyDown("w")) {
fullAuto = !fullAuto;
}
if ( fullAuto == true) {
fireRate = 0.5;
}
else {
fireRate = 0.5;
}
}
function Reload() {
animation.Play("Reload");
AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1);
var reloadTimer : float = 0.0;
var reloadMax : float = animation.GetClip("Reload").length;
reloadTimer += Time.deltaTime;
if (reloadTimer >= reloadMax) {
clip = clipSize;
totalbullets -= clipSize;
}
}
function Empty() {
AudioSource.PlayClipAtPoint(emptyClickSound, transform.position, 1);
}
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.GetButtonUp("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
animation.Play();
Instantiate(bulletTex[Random.Range(0,3)], 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
// 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
}
}
Your answer
Follow this Question
Related Questions
Help on a pick up/reload script 3 Answers
Gun - Ammo , reloading and UI problem. 1 Answer
Gun with ammo help 1 Answer
How can I shooting script c#? 0 Answers