I am currently working on a game that has an element of 3rd person shooter. I have it set up so wherever the mouse is at the time of the space bar pressed bullets are to fire in that direction. However, when the mouse is over the character or gui I assume it is firing towards then aka backwards because they are the first things the raycast hits. I need some way to have a ray travel forward in the area the mouse is in. This is the code I currently have (used from an example someone else gave, I did not write this myself) :
//TRYING TO ADD GUN FIRE
if (firing == true Time.time > nextFire) {
if (!audio.isPlaying){
audio.clip = gunFireNoise;
audio.Play();
}
nextFire = Time.time + fireRate;
// The target position of the projectile is found by casting a ray from the camera with the mouse position.
// This gives us the point the user clicked on.
ray = Camera.mainCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0.0));
// (Note: We need to declare this variable, because it is modified inside Physics.Raycast)
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, 500)) {
target = hit.point;
}
else {
// If the user clicks outside any object, we assume the target is 1000 units in front of the camera!
target = (ray.origin + ray.direction * 1000);
}
// The direction the projectile should fly along is simply the offset between the target position and the launch position
direction = target - transform.position;
// Instantiate the projectile and its entire transform hierarchy
// (Note: We need to declare this variable because Instantiate can't tell which type it will return)
var instantiatedProjectile : GameObject = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.forward, direction));
// Give it an initial velocity
instantiatedProjectile.rigidbody.velocity = direction.normalized * speed;
// Destroy the projectile in a few seconds.
// Destroying the game object kills the game object
// with all components and its transform hierarchy.
Destroy (instantiatedProjectile, autoDestroySeconds);
}
//END OF GUN FIRE
}
Here is also a picture that might help explain what it is I mean. The right square kinda gives an idea of what the screen looks like when I have the issue while the left shows what I assume is happening. The dot in right corner is where objects are instantiated from.

I have a feeling that you want to look at Physics.RaycastAll. This will give you back a list of colliders. loop through them until you find the first collider that is NOT your player. (GUI object should not affect raycasts)
Thanks for the fast reply. I was considering raycast all since I do have various tags on everything, and even tried to code it. However I was getting errors and it seems I lack the knowledge of the code to check the tags of GameObjects hit by the raycast. Do you have a snippet of code or just know how to right out that line or two? I’ve searched on here but can find an example.
The example is probably best.
Can you post what you have?
Well I couldn’t get it running at all when I tried so I went back to what I had, but This is about as close as I think I can get…
ray = Camera.mainCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0.0));
// (Note: We need to declare this variable, because it is modified inside Physics.Raycast)
var hits : RaycastHit[];
if (Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), hits, 500)) {
for (var i = 0;i < hits.Length; i++) {
var hit : RaycastHit = hits[i];
if(hit.tag != "Player"){
target = hit.point;
}
}
}
else {
// If the user clicks outside any object, we assume the target is 1000 units in front of the camera!
target = (ray.origin + ray.direction * 1000);
}
// The direction the projectile should fly along is simply the offset between the target position and the launch position
direction = target - transform.position;
// Instantiate the projectile and its entire transform hierarchy
// (Note: We need to declare this variable because Instantiate can't tell which type it will return)
var instantiatedProjectile : GameObject = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.forward, direction));
// Give it an initial velocity
instantiatedProjectile.rigidbody.velocity = direction.normalized * speed;
This gives me two errors,
- no appropriate version of raycast all for argument list
- tag not a member of raycast hit
Yeah, your thinking RaycastAll is the same as Raycast… it is not… it returns an array of RaycastHit’s So there is no true or false.
var ray : Ray = Camera.mainCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0.0));
target = ray.GetPoint(1000);
// (Note: We need to declare this variable, because it is modified inside Physics.Raycast)
var hits : RaycastHit[];
hits=Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), 500);
for (var i = 0;i < hits.Length; i++) {
var hit : RaycastHit = hits[i];
if(hit.collider.gameObject.tag != "Player"){
target = hit.point;
}
}
// The direction the projectile should fly along is simply the offset between the target position and the launch position
//direction = target - transform.position;
// Instantiate the projectile and its entire transform hierarchy
// (Note: We need to declare this variable because Instantiate can't tell which type it will return) // it returns a GameObject
var instantiatedProjectile : Transform = Instantiate(projectile, projectile.position, Quaternion.identity).transform;
//FromToRotation (Vector3.forward, direction)
instantiatedProjectile.LookAt(target);
// Give it an initial velocity
instantiatedProjectile.rigidbody.velocity = instantiatedProjectile.forward * speed;
I also cleaned up some other loose ends.
Wow thanks for all your help so far first of all! I put in the revised code but it doesn’t like the line…
var instantiatedProjectile : Transform = Instantiate(projectile, projectile.position, Quaternion.identity).transform;
It’s saying position is not a member of GameObject. I tried switching it to transform but it then it goes to the no appropriate version error again. Any suggestions?