- Home /
RayCast "blueline" qoes down
Hallo. I want to create FPS. I was finding gun shoot script for 2 days. I found narutoisgreat1234's Raycast shoot script. Here is it with my small modifications. :`
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 20;
var BulletPerClip : int = 60;
var RelaodTime : float = 3.3;
var Damage : int = 10;
var MoveSpeed: float = 1;
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.5;
public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;
public var muzzleFlash : ParticleEmitter;
public var hitParticles : ParticleEmitter;
public var muzzleFlashTimer : float = 0;
public var KeyCooler : float = 0.5;
public var KeyTimer : float = 0;
public var muzzleFlashCooler : float = 0.5;
public var Lights1 : GameObject;
public var Lights2 : GameObject ;
public var Lights3 : GameObject;
function Start(){
BulletsLeft = BulletPerClip;
muzzleFlash.emit = false;
DefaultPos = transform.localPosition;
hitParticles.emit = false;
}
function Update () {
if( KeyTimer > 0){
KeyTimer -= Time.deltaTime;
}
if( KeyTimer < 0){
KeyTimer= 0;
}
if( muzzleFlashTimer > 0){
muzzleFlashTimer -= Time.deltaTime;
MuzzleFlash();
}
if( muzzleFlashTimer < 0){
muzzleFlashTimer = 0;
}
if( ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if(ShootTimer < 0){
ShootTimer = 0;
}
if( KeyTimer == 0){
if(Input.GetMouseButton(0) && BulletsLeft ){
if( ShootTimer == 0){
PlayShootAudio();
RayShoot();
ShootTimer = ShootCooler;
KeyTimer = KeyCooler;
}
if( muzzleFlashTimer == 0){
muzzleFlashTimer = muzzleFlashCooler;
MuzzleFlash ();
}
}
}
}
function MuzzleFlash (){
if( muzzleFlashTimer > 0){
muzzleFlash.emit = false;
Lights1.active = false;
Lights2.active = false;
Lights3.active = false;
}
if( muzzleFlashTimer == muzzleFlashCooler){
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * 2000000, Vector3.forward);
muzzleFlash.emit = true;
Lights1.active = true;
Lights2.active = true;
Lights3.active = true;
}
}
function RayShoot (){
//GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Shoot"); //(if your player has animations then remove the // infront of the GameObject.Find and then
//(replace your objetcs name there. and then the animation name)
var hit : RaycastHit;
var direction : Vector3 = transform.TransformDirection( Vector3.forward);
Debug.DrawRay(transform.position , direction * Range , Color.blue);
if(Physics.Raycast(transform.position , direction , hit, Range)){
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.rigidbody){
if( hitParticles){
hitParticles.transform.position = hit.point;
hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
hit.collider.SendMessageUpwards("ApplyDamage" , Damage , SendMessageOptions.DontRequireReceiver);
}
}
}
BulletsLeft --;
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if( BulletsLeft == 0 ){
Reload();
}
}
function Reload (){
PlayReloadAudio();
yield WaitForSeconds(0.2);
//GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Reload"); //(if your player has animations then remove the // infront of the GameObject.Find and then
//(replace your objetcs name there. and then the animation name)
yield WaitForSeconds( RelaodTime);
if(Clips > 0){
BulletsLeft = BulletPerClip;
Clips -= 1;
}
}
function PlayShootAudio(){
audio.PlayOneShot( ShootAudio);
}
function PlayReloadAudio(){
audio.PlayOneShot( ReloadAudio);
}
` Now what's my problem. When i shoot, drawray (debug blue line) goes down and i can't interact with others things because drawray is down. Thanks in advance !
Can you edit your question and format the code by indenting it 4 spaces or 1 tab before pasting - it's unreadable like that.
@OrangeLightning - formatting the code is horrible now isn't it? Since UDN suddenly all the damn symbols are escaped...
Its probably something with parenting I got the problem all the time, you should fiddle with the parents of the gun, or attach the script to another gameobject.
@Hybris -> Ok i will try that. Did you solved it in your project? BTW ALL: Sorry for my bad formating and thank for editting !
I did solve it, BTW sometimes the drawray doesnt work, then you just have to delete the drawray, and it'll work
Answer by DevSpace · Jul 14, 2012 at 11:45 PM
This is a very improved version of your script with many new features like bullet holes and sparks and mode like automatic, burst and single shot. if the ray keep going down rotate your weapon because the ray only goes to Z, and if your Z is set to down the weapon shoot the ray to down every time.
In this script, if the shoot goes to back change
var sprayZ = 1.0;to
var sprayZ = -1.0;.
Shooting.js
var defaultHolePrefab : GameObject; var metalHolePrefab : GameObject; var woodHolePrefab : GameObject; var brickHolePrefab : GameObject; var dirtHolePrefab : GameObject; var mudHolePrefab : GameObject; var bloodHolePrefab : GameObject;
var defaultSparkPrefab : GameObject; var metalSparkPrefab : GameObject; var woodSparkPrefab : GameObject; var brickSparkPrefab : GameObject; var dirtSparkPrefab : GameObject; var mudSparkPrefab : GameObject; var bloodSparkPrefab : GameObject;
//Lights should be setup in a 3 point lighting system (Triangular setup) around the muzzle flash. Set Range to 1 var muzzleFlash : GameObject; var muzzleOrangeLight : GameObject; //Very light orange, point light var muzzleWhiteLight : GameObject; var muzzleBlueLight : GameObject; //Very light blue, point light. Almost white
var shootAudio : AudioClip; var reloadAudio : AudioClip;
var isPrimary : boolean = false; //Used strictly for the pick up script for proper ammo pick up or weapon replacement var isSecondary : boolean = false; //Used strictly for the pick up script for proper ammo pick up or weapon replacement
var isSingleShot : boolean = false; var isAutomatic : boolean = false; var isBurst : boolean = false;
private var isAiming : boolean = false; private var isReloading : boolean = false;
var CurAmmo : int; var AmmoPerClip : int; var AmmoLeft : int; var MaxAmmo : int; var Damage : int; var Range : float = 1000; var Force : float = 1000; var ReloadTime : float = 3; var shotSpread : float = 0.2; var ShootSpeed : float = 0.15; //time between each shot. Lower the number, faster the burst
private var AmmoToLoad : int; private var ShootTimer : float = 0; private var ShootCooler : float = 0.3; private var shotTime : float = 0; //Must always remain at 0 private var burstCount : float = 0; // Must always remain at 0 private var flashLength : float = 0.1; //MuzzleFlash Length private var flashScale : float = 0.25; //Max MuzzleFlash Scale private var NextShot : float;
function Start() { CurAmmo = AmmoPerClip; AmmoLeft = MaxAmmo; NextShot = ShootSpeed; muzzleFlash.active = false; muzzleOrangeLight.active = false; muzzleWhiteLight.active = false; muzzleBlueLight.active = false; }
function Update() { if(AmmoLeft < 0) { CurAmmo += AmmoLeft; AmmoLeft = 0; } if(CurAmmo > AmmoPerClip) { CurAmmo = AmmoPerClip; } if(ShootTimer > 0) { ShootTimer -= Time.deltaTime; } if(ShootTimer > 0) { ShootTimer = 0; } if(Input.GetKeyDown(KeyCode.Mouse0) && isSingleShot) { if(ShootTimer == 0) { if(CurAmmo > 0 && isAiming == false) { if(isReloading == false) { HipShoot(); PlayShootAudio(); MuzzleFlash(); ShotTimer = ShootCooler; } } if(CurAmmo > 0 && isAiming == true) { if(isReloading == false) { AimShoot(); PlayShootAudio(); MuzzleFlash(); ShootTimer = ShootCooler; } } } } if(Input.GetKey(KeyCode.Mouse0) && isAutomatic) { if(CurAmmo > 0 && isAiming == false) { shotTime += Time.deltaTime; if(shotTime >= NextShot) { if(isReloading == false) { NextShot += ShootSpeed; PlayShootAudio(); MuzzleFlash(); HipShoot(); } } } if(CurAmmo > 0 && isAiming == true) { shotTime += Time.deltaTime; if(shotTime >= NextShot) { if(isReloading == false) { NextShot += ShootSpeed; PlayShootAudio(); MuzzleFlash(); AimShoot(); } } } } if(Input.GetKey(KeyCode.Mouse0) && isBurst && burstCount < 3) { if(CurAmmo > 0 && isAiming == false) { shotTime += Time.deltaTime; if(shotTime >= NextShot) { if(isReloading == false) { NextShot += ShootSpeed; burstCount += 1; PlayShootAudio(); MuzzleFlash(); HipShoot(); } } } if(CurAmmo > 0 && isAiming == true) { shotTime += Time.deltaTime; if(shotTime >= NextShot) { if(isReloading == false) { NextShot += ShootSpeed; burstCount += 1; PlayShootAudio(); MuzzleFlash(); AimShoot(); } } } } if(Input.GetKeyUp(KeyCode.Mouse0)) { NextShot = 0; shotTime = 0; if(isBurst == true) { burstCount = 0; } } if(Input.GetKeyDown(KeyCode.R) && CurAmmo > 0) { if(AmmoLeft > 0 && CurAmmo < AmmoPerClip) { if(isReloading == false) { Reload(); } } } if(Input.GetKeyDown(KeyCode.Tab) && isAiming == false) { Aim(); } if(Input.GetKeyUp(KeyCode.Tab)) { Return(); } }
function HipShoot() { var Hit : RaycastHit; var DirectionRay = SprayDirection(); Debug.DrawRay(transform.position, DirectionRay Range, Color.blue); if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)) { var hitRotation = Quaternion.FromToRotation(Vector3.up, Hit.normal); if(Hit.transform.tag == "Untagged") { var defaultHole = Instantiate(defaultHolePrefab, Hit.point, hitRotation) as GameObject; defaultHole.transform.parent = Hit.transform; if(defaultSparkPrefab) { Instantiate(defaultSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Metal") { var metalHole = Instantiate(metalHolePrefab, Hit.point, hitRotation) as GameObject; metalHole.transform.parent = Hit.transform; if(metalSparkPrefab) { Instantiate(metalSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Wood") { var woodHole = Instantiate(woodHolePrefab, Hit.point, hitRotation) as GameObject; woodHole.transform.parent = Hit.transform; if(woodSparkPrefab) { Instantiate(woodSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Brick") { var brickHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject; brickHole.transform.parent = Hit.transform; if(brickSparkPrefab) { Instantiate(brickSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Dirt") { var dirtHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject; dirtHole.transform.parent = Hit.transform; if(dirtSparkPrefab) { Instantiate(dirtSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Mud") { var mudHole = Instantiate(mudHolePrefab, Hit.point, hitRotation) as GameObject; mudHole.transform.parent = Hit.transform; if(mudSparkPrefab) { Instantiate(mudSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Enemy") { var bloodHole = Instantiate(bloodHolePrefab, Hit.point, hitRotation) as GameObject; bloodHolePrefab.transform.parent = Hit.transform; if(bloodSparkPrefab) { Instantiate(bloodSparkPrefab, Hit.point, hitRotation); } } if(Hit.rigidbody) { Hit.rigidbody.AddForceAtPosition(DirectionRay Force, Hit.point); Hit.collider.SendMessageUpwards ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver); } } CurAmmo --; AmmoToLoad ++; if(CurAmmo < 0) { CurAmmo = 0; } if(CurAmmo == 0 && AmmoLeft > 0) { Reload(); } }
function AimShoot() { var Hit : RaycastHit; var DirectionRay = transform.TransformDirection(Vector3.forward); Debug.DrawRay(transform.position, DirectionRay Range, Color.blue); if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)) { var hitRotation = Quaternion.FromToRotation(Vector3.up, Hit.normal); if(Hit.transform.tag == "Untagged") { var defaultHole = Instantiate(defaultHolePrefab, Hit.point, hitRotation) as GameObject; defaultHole.transform.parent = Hit.transform; if(defaultSparkPrefab) { Instantiate(defaultSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Metal") { var metalHole = Instantiate(metalHolePrefab, Hit.point, hitRotation) as GameObject; metalHole.transform.parent = Hit.transform; if(metalSparkPrefab) { Instantiate(metalSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Wood") { var woodHole = Instantiate(woodHolePrefab, Hit.point, hitRotation) as GameObject; metalHole.transform.parent = Hit.transform; if(woodSparkPrefab) { Instantiate(woodSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Brick") { var brickHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject; brickHole.transform.parent = Hit.transform; if(brickSparkPrefab) { Instantiate(brickSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Dirt") { var dirtHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject; dirtHole.transform.parent = Hit.transform; if(dirtSparkPrefab) { Instantiate(dirtSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Mud") { var mudHole = Instantiate(mudHolePrefab, Hit.point, hitRotation) as GameObject; mudHole.transform.parent = Hit.transform; if(mudSparkPrefab) { Instantiate(mudSparkPrefab, Hit.point, hitRotation); } } if(Hit.transform.tag == "Enemy") { var bloodHole = Instantiate(bloodHolePrefab, Hit.point, hitRotation) as GameObject; bloodHolePrefab.transform.parent = Hit.transform; if(bloodSparkPrefab) { Instantiate(bloodSparkPrefab, Hit.point, hitRotation); } } if(Hit.rigidbody) { Hit.rigidbody.AddForceAtPosition(DirectionRay Force , Hit.point); Hit.collider.SendMessageUpwards ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver); } } CurAmmo --; AmmoToLoad ++; if(CurAmmo < 0) { CurAmmo = 0; } if(CurAmmo == 0 && AmmoLeft > 0) { Reload(); } }
function Reload() { isReloading = true; audio.PlayOneShot(reloadAudio); //Add Reload Animation yield WaitForSeconds(ReloadTime); isReloading = false; CurAmmo += AmmoToLoad; AmmoLeft -= AmmoToLoad; AmmoToLoad = 0; }
function SprayDirection() { var sprayX = (1 - 2 Random.value) shotSpread; var sprayY = (1 - 2 Random.value) shotSpread; var sprayZ = 1.0; return transform.TransformDirection(Vector3(sprayX, sprayY, sprayZ)); }
function Aim() { //Add Aim Animation isAiming = true; }
function Return() { //Add Reverse Aim Animation isAiming = false; }
function PlayShootAudio() { audio.PlayOneShot(shootAudio); }
function MuzzleFlash() { muzzleFlash.transform.eulerAngles.x = Random.Range(0.0, 15.0); muzzleFlash.transform.localScale.x = 1 + Random.Range(-flashScale, flashScale); //Resets the scale muzzleFlash.active = true; muzzleOrangeLight.active = true; muzzleWhiteLight.active = true; muzzleBlueLight.active = true; yield WaitForSeconds(flashLength); muzzleFlash.active = false; muzzleOrangeLight.active = false; muzzleWhiteLight.active = false; muzzleBlueLight.active = false; }
$$anonymous$$ight I advise you not to just paste your scripts in a answer, the point is that they learn not copy paste scripts.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need a script for a gun that shoots bullet using raycast 2 Answers
Script keeps functioning when disabled 1 Answer
RayCast Problem : 2d 1 Answer
Help with making item invisible if not on screen 1 Answer
Scripted Door w/ Line of Sight 1 Answer