I’ve got an object I wish to duplicate many times. I don’t want to use layers because I want the raycast to hit the duplicate’s SphereCollider, just not each one’s own collider.
Tried to move the origin of the raycast to the edge of collider, but so far I have been unsuccessful. Do I need to RaycastAll? Can someone point me in the right direction?
Here’s a simple example C# script. It continually hits itself.
using UnityEngine;
using System.Collections;
public class SimpleAI : MonoBehaviour {
public float speed = 20;
public float rotateSpeed = 10;
public RaycastHit hit;
void Start () {
//
}
// Update is called once per frame
void Update () {
//SphereCollider mCollider = (SphereCollider)GetComponent(typeof(SphereCollider));
//Vector3 adjOrigin = transform.position + Vector3.forward * mCollider.radius;
if(!Physics.Raycast(transform.position, transform.forward, out hit, 10)){
transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
} else {
if(hit.collider.gameObject.tag == "npc"){
Debug.Log("I hit myself, or someone in my group.");
} else if(hit.collider.gameObject.tag == "player"){
Debug.Log("I hit the player");
transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime);
} else if(hit.collider.gameObject.tag == "ground"){
Debug.Log("I hit the ground.");
} else {
Debug.Log("Something is seriously messed up.");
}
}
}
}
Now you simply have to ignore ‘ownCollider’ when checking the array you are getting with ‘RaycastAll’.
It’s straight forward, as long as you don’t get performance issues.
I’m having a similar issue to the OP, in that I’m trying to prevent a raycast (fired from a hand-held weapon) from detecting the collider of the parent object (Player) that it is attached to.
I’ve gotten as far as creating a private Collider variable that is set to the root object’s collider (in this case, the Player), then only applying damage if the raycast doesn’t hit the Player’s collider. Code snippet below:
public class BurstRifle : MonoBehaviour
{
private Collider ownCollider;
void Start ()
{
// Set the weapon owner's collider as ownCollider.
ownCollider = transform.root.gameObject.GetComponent<Collider>();
}
void FireOneShot ()
{
Vector3 direction= transform.TransformDirection(Vector3.forward);
RaycastHit hit = new RaycastHit();
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, out hit, range))
{
// Test to ensure we don't damage ourselves.
if (!ownCollider)
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
Using the above code, the Player’s gunshots no longer hurts himself… but it now does no damage to enemies either. I’m not 100% familiar with raycasting code, but I’m guessing that I need to use RaycastAll to get the gunshots to register on enemy colliders if I’m ignoring the Player’s own collider.
After many hours(!) trying to make heads from tails of the RaycastAll API document, I really have no idea how to convert my existing RaycastHit behaviour into RaycastAll, especially as RaycastAll looks like it also uses arrays (another C# coding syntax that I haven’t fully grasped yet).
Any conversion help, or even a better explanation as to why my code is preventing gunshots from damaging enemy colliders, would be much appreciated!