Quantcast
Viewing all articles
Browse latest Browse all 9

Automatic shooting not working?

Hi, So my automatic shooting is not working, here is the script: #pragma strict var drawCrosshair = true; var crosshairColor = Color.white; var width : float = 3; var height : float = 35; var curAmmoInClip : int; var curAmmo : int; var ammoInClip : int = 30; var maxAmmo : int = 120; var startAmmo : int = 120; var fireRate : float = 3; var defaultParticle : GameObject; var enemyParticle : GameObject; var gunAimLayer : GunAim; var damage = 5; var shotSound : AudioClip; var reloadSound : AudioClip; var muzzleFlash : ParticleEmitter; var muzzleTimer : float = 0.0f; var muzzleCooler : float = 0.1f; var aimPose : Vector3; private var nextFire : float = 0; private var isAiming : boolean = false; class spreading1 { var spread = 20.0; var maxSpread = 60.0; var minSpread = 20.0; var spreadPerSecond = 30.0; var decreasePerSecond = 25.0; } var spread : spreading1; private var tex : Texture2D; private var lineStyle : GUIStyle; function Awake () { tex = Texture2D(1,1); SetColor(tex, crosshairColor); lineStyle = GUIStyle(); lineStyle.normal.background = tex; curAmmo = startAmmo; curAmmoInClip = ammoInClip; } function OnEnable() { gunAimLayer.aimPos = aimPose; gunAimLayer.gunSource = gameObject; } function Update () { if(Input.GetMouseButtonDown(1) && !GetComponent.().IsPlaying("Reload")) isAiming = !isAiming; if(isAiming) { width = 0; height = 0; } else { width = 1; height = 6; } if(Input.GetMouseButton(0)) { spread.spread += spread.spreadPerSecond * Time.deltaTime; } else { spread.spread -= spread.decreasePerSecond * Time.deltaTime; } /*if(Input.GetMouseButtonDown(0)) { if(curAmmoInClip > 0 && !GetComponent.().IsPlaying("Reload")) Fire(); }*/ if(Input.GetMouseButton(0)) { if(curAmmoInClip > 0 && !GetComponent.().IsPlaying("Reload") && Time.time > nextFire) Fire(); } if(Input.GetKeyDown("r")) { if(curAmmo > 0 && curAmmoInClip < ammoInClip && !GetComponent.().IsPlaying("Reload") && !isAiming) Reload(); } if(muzzleTimer > 0 && muzzleFlash) { muzzleFlash.emit = false; } if(curAmmoInClip <= 0) { CancelInvoke(); } spread.spread = Mathf.Clamp(spread.spread, spread.minSpread, spread.maxSpread); muzzleTimer -= Time.deltaTime; } function OnGUI () { var centerPoint = Vector2(Screen.width / 2, Screen.height / 2); if(drawCrosshair) { GUI.Box(Rect(centerPoint.x - width / 2, centerPoint.y - (height + spread.spread), width, height), "", lineStyle); GUI.Box(Rect(centerPoint.x - width / 2, centerPoint.y + spread.spread, width, height), "", lineStyle); GUI.Box(Rect(centerPoint.x + spread.spread, (centerPoint.y - width / 2), height , width), "", lineStyle); GUI.Box(Rect(centerPoint.x - (height + spread.spread), (centerPoint.y - width / 2), height , width), "", lineStyle); } GUILayout.Box(curAmmoInClip + " / " + curAmmo); } function Fire() { var hit : RaycastHit; var offset = 0f; if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) { if(Input.GetKey(KeyCode.LeftShift)) offset = Random.Range(-0.1f, 0.1f); else { if(isAiming) offset = Random.Range(-0.02f, 0.02f); else offset = Random.Range(-0.03f, 0.03f); } } if(!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { if(isAiming) offset = Random.Range(-0.01f, 0.01f); else offset = Random.Range(-0.015f, 0.015f); } var ray : Ray = new Ray(Camera.main.transform.position + new Vector3(offset, -offset, 0), Camera.main.transform.forward + new Vector3(offset, -offset, 0)); if (Physics.Raycast (ray, hit, 100)) { var otherObj : GameObject = hit.collider.gameObject; if (otherObj.tag == "MeleeEnemy") { otherObj.transform.root.GetComponent(typeof(MeleeAI)).CanDie(damage); var particle = Instantiate(enemyParticle, hit.point + hit.normal * 0.001, Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(particle, 5f); } if (otherObj.tag == "MeleeEnemyHead") { otherObj.transform.root.GetComponent(typeof(MeleeAI)).CanDie(damage * 2); var particle1 = Instantiate(enemyParticle, hit.point + hit.normal * 0.001, Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(particle, 5f); } if (otherObj.tag == "RangeEnemy") { otherObj.transform.root.GetComponent(typeof(ShootingAI)).CanDie(damage); var particle3 = Instantiate(enemyParticle, hit.point + hit.normal * 0.001, Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(particle, 5f); } if (otherObj.tag == "RangedEnemyHead") { otherObj.transform.root.GetComponent(typeof(ShootingAI)).CanDie(damage * 2); var particle4 = Instantiate(enemyParticle, hit.point + hit.normal * 0.001, Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(particle, 5f); } if(otherObj.tag == "Untagged") { var particle2 = Instantiate(defaultParticle, hit.point + hit.normal * 0.001, Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(particle, 5f); } if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(500 * transform.forward, hit.point); } if(muzzleTimer < 0) { muzzleFlash.Emit(); } curAmmoInClip--; muzzleTimer = muzzleCooler; GetComponent.().PlayOneShot(shotSound, Random.Range(0.6f, 0.8f)); switch(Random.Range(1, 2)) { case 1: GetComponent.().CrossFadeQueued("Fire", 0.3f, QueueMode.PlayNow); case 2: GetComponent.().CrossFadeQueued("Fire2", 0.3f, QueueMode.PlayNow); } nextFire = Time.time + fireRate; } function Reload() { GetComponent.().CrossFadeQueued("Reload", 0.3, QueueMode.PlayNow); GetComponent.().PlayOneShot(reloadSound); yield WaitForSeconds(GetComponent.()["Reload"].length); if(curAmmo < ammoInClip) { if(ammoInClip - curAmmoInClip < curAmmo) { curAmmo -= ammoInClip - curAmmoInClip; curAmmoInClip += ammoInClip - curAmmoInClip; } else { curAmmoInClip += curAmmo; curAmmo = 0; } } else { curAmmo -= ammoInClip - curAmmoInClip; curAmmoInClip += ammoInClip - curAmmoInClip; } } function SetColor(myTexture : Texture2D, myColor : Color) { for (var y : int = 0; y < myTexture.height; ++y) { for (var x : int = 0; x < myTexture.width; ++x) { myTexture.SetPixel(x, y, myColor); } } myTexture.Apply(); } I tried different values for fire rate but it always fires very fast ( I think it fires every frame but I'm not 100% sure ). Best regards, Ivan!

Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>