- Home /
Raycast Shooting problem
Hello:) I have a problem with my raycast shooting script, it doesn't shoot:/ This is my script:
var bulletTex : GameObject[];
var TheDammage = 100;
var FireRate : float = 0.1;
var gunFired = false;
private var Canfire = true;
var shot : AudioClip;
var currentBullets = 20;
private var nextFire : float = 0.0;
var fireRate : float = 1.0;
var clips = 5.0;
var ammoCount = 20;
var ammoInMagazine = 20;
var reloadTime = 1.5;
private var t : Transform;
function Start () {
}
function Update () {
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 10000, Color.green);
if(Input.GetButton ("Fire1") && IsOkToShoot() && Physics.Raycast(transform.position, fwd, hit, 10000)){
Shoot();
}
}
function IsOkToShoot () : boolean {
var itsOk : boolean = false;
if (Time.time>nextFire) {
nextFire = Time.time + fireRate;
itsOk = true;
}
return itsOk;
}
function Shoot() {
var hit : RaycastHit;
if (currentBullets > 0) {
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
AudioSource.PlayClipAtPoint(shot, t.position);
currentBullets--; //subtract one
}
if (currentBullets <= 0){
audio.Play();
Reload();
}
}
function Reload() {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
if (clips > 0) {
clips--;
currentBullets = ammoInMagazine;
}
}
Thank you for any response, sorry for my bad English, I'm from Czech republic :)
Answer by Lazdude17 · Jul 03, 2014 at 05:47 PM
Which raycast isn't working? One thing to note is that TransformDirection.forward will not always be what you want it to be because to truly be forward all the time it has to be local coordinates. So if your gun or whatever you are actually shooting from has local coordinates that are different than expected then the raycast will just shoot wherever the +Z axis points. So just tick that local/global button at the top of unity to switch coordinate views and make sure that the Z is pointing where you want to shoot.
Is it doing anything? Have you tried going through and debugging with print statements?
Answer by Juice-Tin · Jul 04, 2014 at 07:12 PM
Hi! I have some helpful raycast debugging functions you can try. Put these in your class, and then use ShowRaycast in your 'if' statement instead of Physics.Raycast. It works the same but will always display a red line of the raycast if nothing is hit, and a green line if something is hit. The line is drawn perfectly on the actual raycast, so you should be able to find your problem a lot easier. :)
(You can remove your Debug.drawRay, as this will auto draw it for you)
public void DrawRay2(Vector3 start, Vector3 dir, float dist, Color color){
Debug.DrawLine(start, start + dir.normalized * dist, color);
}
public bool ShowRaycast(Vector3 start, Vector3 dir, float dist){
bool raycast = Physics.Raycast(start, dir, dist);
DrawRay2(start, dir, dist, raycast?Color.green:Color.red);
return raycast;
}
public bool ShowRaycast(Vector3 start, Vector3 dir, out RaycastHit raycastHit, float dist){
bool raycast = Physics.Raycast(start, dir, out raycastHit, dist);
DrawRay2(start, dir, dist, raycast?Color.green:Color.red);
return raycast;
}
Your answer
Follow this Question
Related Questions
Raycast damage script 2 Answers
change to raycast shooting instead of rigidbody shooting 1 Answer
Raycast Shooting Help 1 Answer
NEED HELP WITH RAYCAST SHOOTING !!! 1 Answer
need help converting gun script from c# to javascript 1 Answer