Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Prevent Bullets from going through objects/ No raycast

$
0
0
I have 2 Scripts 1 is attached to the gun the other to the bullet. The problem is, (and i really looked everywhere but nothing worked) that the bullets sometimes are going through objects. My scripts: GUN: #pragma strict var Bullet : Rigidbody; var FirePoint : Transform; var shot : AudioClip; var FireType : int = 0; // 0 = Auto, 1 = Single, 2 = burst private var autoTimer : float; private var singleTimer : float; var myTimer : float; var myTimerSingle : float; var BurstTimer : float; var BurstCount : int; private var myTimerBurst : float = 0.075; var BurstFired = false; var BurstReload : float = 1.5; var force: float = 2.5; // controls recoil amplitude var upSpeed: float = 9; // controls smoothing speed var dnSpeed: float = 20; // how fast the weapon returns to original position private var ang0: Vector3; // initial angle private var targetX: float; // unfiltered recoil angle private var ang = Vector3.zero; // smoothed angle function Recoil() { targetX += force; // add recoil force } function Start() { autoTimer = myTimer; singleTimer = myTimerSingle; BurstTimer = myTimerBurst; ang0 = transform.localEulerAngles; } function Update() { ang.x = Mathf.Lerp(ang.x, targetX, upSpeed * Time.deltaTime); transform.localEulerAngles = ang0 - ang; // move the camera or weapon targetX = Mathf.Lerp(targetX, 0, dnSpeed * Time.deltaTime); // returns to rest if(Input.GetKeyDown(KeyCode.T)) { FireType += 1; } if(FireType == 3) { FireType = 0; } if(autoTimer > 0) { autoTimer -= 1*Time.deltaTime; } if(autoTimer <= 0) { autoTimer = 0; } if(singleTimer > 0) { singleTimer -= 1*Time.deltaTime; } if(singleTimer <= 0) { singleTimer = 0; } if(BurstTimer > 0) { BurstTimer -= 1*Time.deltaTime; } if(BurstTimer <= 0) { BurstTimer = 0; } if(BurstCount == 0) { BurstCount = 3; BurstFired = false; BurstReload = 0.3; } if(BurstReload > 0) { BurstReload -= 1*Time.deltaTime; } if(BurstReload < 0) { BurstReload = 0; } } function FixedUpdate () { if(Input.GetButtonDown("Fire1") && FireType == 1 && singleTimer == 0 && Ammo.ClipAmmo > 0) { var BulletInstance : Rigidbody; BulletInstance = Instantiate(Bullet, FirePoint.position, transform.rotation); BulletInstance.AddForce(FirePoint.forward * 50); audio.PlayOneShot(shot, 0.1); singleTimer = myTimerSingle; Recoil(); Ammo.ClipAmmo -= 1; } if(Input.GetButton("Fire1") && FireType == 0 && autoTimer == 0 && Ammo.ClipAmmo > 0) { BulletInstance = Instantiate(Bullet, FirePoint.position, FirePoint.rotation); BulletInstance.AddForce(FirePoint.forward * 30000); audio.PlayOneShot(shot, 0.1); autoTimer = myTimer; Recoil(); Ammo.ClipAmmo -= 1; } if(BurstFired == true && FireType == 2 && BurstTimer == 0 && BurstCount > 0 && BurstReload == 0 && Ammo.ClipAmmo > 0) { BulletInstance = Instantiate(Bullet, FirePoint.position, FirePoint.rotation); BulletInstance.AddForce(FirePoint.forward * 30000); audio.PlayOneShot(shot, 0.1); BurstCount -= 1; BurstTimer = myTimerBurst; Recoil(); Ammo.ClipAmmo -= 1; } if(Input.GetButtonDown("Fire1") && FireType == 2) { BurstFired = true; } } Bullet: #pragma strict var lifeTime : float = 10; function OnCollisionEnter () { Destroy(gameObject); } function FixedUpdate () { rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; } function Update() { lifeTime -= 1*Time.deltaTime; if(lifeTime <= 0) { Destroy(gameObject); } } if anyone has any idea how to fix the problem it would be awesome, i already set the FixedTimestep to 0.01 and i dont want to use raycast because i want the bullets to go down with the time(gravity) thanks in advance skullbeats1

Viewing all articles
Browse latest Browse all 592

Trending Articles



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