- Home /
My raycast doesn't fire
Hello there. I'll keep it brief. I'm trying to make RTS-like Citizen Selection, but my raycast does not seem to be firing when I left click.
#pragma strict
function Update() {
if(Input.GetMouseButtonDown (0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100.0)) {
return hit.collider.gameObject;
}
Debug.Log(hit);
}
}
It just doesn't seem to be firing at all, and I'm really stumped as to why. Thanks in advance.
Answer by r0b3rt97 · Oct 09, 2013 at 10:41 PM
well there's more ways to shot raycast. in your gun script (if you have one) type
bullet variable should be empty prefab with only bullet script, and bulletSpawn should be empty gameObject that represent position where you want bullet to be spawned when you shot var bullet : GameObject; var bulletSpawn : Transform;
if(Input.GetButtonDown("Fire1")) { Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation); }
then in bullet script type:
var maximumDistance= 500;
function Update() { var bullet : RaycastHit; if(Physics.Raycast(transform.position, transform.forward, bullet,maximumDistance)) { //to make sure your raycast works properly print("Raycast is shooting"); } Destroy(gameObject); }
Your answer
Follow this Question
Related Questions
Particle Instantiation problem. 0 Answers
Click&Drag Misterious Disappearing! 1 Answer
how to make a raycast have a wider range of hit 2 Answers
setting camera to hit.point returning null vector 1 Answer
Please help me with Raycast 1 Answer