- Home /
How to fire a gun using raycasting and still add force
Hi. I have a gun code that I wrote and I was wondering if there was a way to make the rigidbodies that I shoot have a force applied at the point at which the raycast hit.
If necessary, here is my code:
enum FireType { RayCast, Projectile } enum ShotType { Semi_Auto, Full_Auto, } var Clips : int;
var clipSize : int;
var unlimited : boolean = false;
var autoReload : boolean = true;
var reloadTime : int;
var gunRange : int;
var fireRate : float;
var projectilePrefab : Rigidbody;
var projectileShotPower : float;
var Damage : int;
var fireType : FireType;
var shotType : ShotType;
var shotSound : AudioClip;
var OutOfAmmoSound : AudioClip;
var reloadSound : AudioClip;
var ShotEmitter : ParticleEmitter[];
var tipOfGun : Transform;
var centerScreen : Transform;
var WoodParticle : GameObject;
var MetalParticle : GameObject;
var ConcreteParticle : GameObject;
var DirtParticle : GameObject;
var SnowParticle : GameObject;
var WaterParticle : GameObject;
var EnemyParticle : GameObject;
var gunName : String;
var bulletMark : GameObject;
var gunCamera : Camera;
var guiTex : GUITexture;
var CrossHeir : Texture2D;
var crossHeirChangeColor : Color;
var reloadElapse : float;
var pushPower : float;
private var Audio : AudioSource; private var freeToShoot : boolean = true; private var freeToReload : boolean = true; private var shotsLeft : int; private var clipsLeft : int;
function Awake() { for(var i = 0; i < ShotEmitter.length; i++) { ShotEmitter[i].emit = false; } Audio = gameObject.GetComponent(AudioSource); shotsLeft = clipSize; clipsLeft = Clips; } function Update () { if(Input.GetButton("Fire1")) { if(fireType == FireType.RayCast) { if(shotType == ShotType.Semi_Auto) { Shoot(true, false); } else if (shotType == ShotType.Full_Auto) { Shoot(false, false); } } else if (fireType == FireType.Projectile) { Shoot(false, true); } } else { freeToShoot = true; }
if(shotsLeft <= 0 && autoReload)
{
Reload();
freeToShoot = false;
} else if (shotsLeft <= 0 && !autoReload)
{
freeToShoot = false;
if(Input.GetButtonDown("Fire2"))
{
Reload();
}
}
if(clipsLeft == 0)
{
freeToReload = false;
}
} function Shoot(oneShot : boolean, isProjectile : boolean) { if(freeToShoot == true) { if(oneShot) { var SAhit : RaycastHit; var SAfwd = transform.TransformDirection(Vector3.forward); GenerateShotGraphics(); Debug.Log("I am about to generate shot graphics"); if(!unlimited) { shotsLeft--; } if(Physics.Raycast(centerScreen.transform.position, SAfwd, SAhit, gunRange)) { if(SAhit.collider.tag != "Enemy") { GenerateHitGraphics(SAhit, SAhit); ApplyForce(SAhit); } else { HitEnemy(SAhit); } } freeToShoot = false; } if(!oneShot) { var nextShot : float; if(Time.time > nextShot) { nextShot = Time.time + fireRate; var FAhit : RaycastHit; var FAfwd = transform.TransformDirection(Vector3.forward); GenerateShotGraphics(); if(!unlimited) { shotsLeft--; } if(Physics.Raycast(centerScreen.transform.position, FAfwd, FAhit, gunRange)) { if(FAhit.collider.tag != "Enemy") { GenerateHitGraphics(FAhit, FAhit); ApplyForce(FAhit); } else { HitEnemy(FAhit); } } } } } } function GenerateHitGraphics(hit : RaycastHit, BMhit : RaycastHit) { var go : GameObject; var delta : float = -0.02; var hitUpDir : Vector3 = hit.normal; if(!BMhit.collider.rigidbody) { var bm = Instantiate(bulletMark, BMhit.point, Quaternion.FromToRotation(Vector3.forward, -BMhit.normal)); bm.AddComponent(BulletMark); } switch(hit.collider.tag) { case "Wood": go = Instantiate(WoodParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; case "Metal": go = Instantiate(MetalParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; case "Snow": go = Instantiate(SnowParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; case "Concrete": go = Instantiate(ConcreteParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; case "Dirt": go = Instantiate(DirtParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; case "water": go = Instantiate(WaterParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; break; } Destroy(go, go.gameObject.GetComponent(ParticleEmitter).maxEnergy); } function GenerateShotGraphics() { for(var i = 0; i < ShotEmitter.length; i++) { ShotEmitter[i].Emit(); } PlayShootSound(); } function HitEnemy(hit : RaycastHit) { var go : GameObject; var delta : float = -0.02; var hitUpDir : Vector3 = hit.normal; go = Instantiate(EnemyParticle, hit.point, Quaternion.FromToRotation(Vector3.forward, hitUpDir)) as GameObject; SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver); Destroy(go, go.gameObject.GetComponent(ParticleEmitter).maxEnergy); } function ApplyForce(hit : RaycastHit) { var body : Rigidbody = hit.collider.rigidbody; if(body) { if(!body.isKinematic) { var direction : Vector3 = hit.collider.transform.position - centerScreen.position; body.AddForceAtPosition(direction.normalized * pushPower, hit.point, ForceMode.Impulse); } } } function Reload() { if(freeToReload) { //Debug.Log("I am reloading"); FreeToShoot = false; shotsLeft = clipSize; clipsLeft--; yield WaitForSeconds(reloadElapse); guiTex.texture = CrossHeir; FreeToShoot = true; } } //Audio functions
function PlayShootSound() { Audio.PlayOneShot(shotSound); }
function PlayOutOfAmmoSound() { Audio.PlayOneShot(OutOfAmmoSound, 1); Debug.Log("Out Of Ammo"); }
function PlayReloadSound() {
Audio.PlayOneShot(reloadSound, 1);
}
I don't get what you really want from us... You already have the function ApplyForce() the uses AddForceAtPosition.
Answer by AVividLight · Feb 11, 2011 at 08:45 PM
hit.rigidbody(addForce);
or something like that...