Hello, so i have made a script and i wanted it to be able to tell if the “Gear”(the object im working with) was colliding with the Same Object and if so then delete it essentially… but it seems that the Script only plays my Debug if my player gameobject walks into its collider is their a way i can tell if its own collider collides with it, im making a voxel game where u place blocks and ive had to Instantiate the said gameobject into the block when its placed so if i place multiple i need to be able to tell if the gameobject beside it is the same object basically… which ive desided to try and achieve with colliders not sure if getting the same object useing colliders is the right way to go about this i tryed useing raycasting but their was a few problems so i scrapped that idea …
This is the code im currently useing to try and tell if the “Gear” is colliding with another Gear.
using UnityEngine;
using System.Collections;
public class GearDetect : MonoBehaviour {
public GameObject Gear;
public GameObject Trigger;
public bool GearCollided = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider GearCollider){
Debug.Log ("Gears Colliding");
}
void OnTriggerStay(Collider GearCollider){
Debug.Log ("Gears Colliding");
if (GearCollider.gameObject.name == "Gear 1") {
GearCollided = true;
Destroy (Gear);
}
}
void OnTriggerExit(Collider GearCollider){
}
}
If the ontriggers are not calling at all, then I am guessing when you place the gear next to the gear, its not actually touching.
What problems did raycasts have?
ok so the OnTriggers are working they just only work when the player is within its collider and i need them to check if the Gears collider is colliding with another Gears Collider … as for the raycasting i found it to be very buggy with what i was trying to do, when i used it sometimes it would completely miss the gear and wouldent Register at all and i wasent sure why so i just moved to trying to figure this out using colliders
** And also i have just checked the Gears colliders are Fairly big so im almost 100% sure their touching
If the Ontrigger only calls when the player touches, but not the gears, then to me that either means the gears are not touching, or you are ignoring their collision through layers or something.
If you move the gears closer manually, does it do anything?
Ok , so i tryed to move them around i litterally put the two inside each other and nothing i belive what you said is true it does seem like the “gears collider” is ignoring itself which is what i dont want its only working if the player is within its collider… i did make a few changes i bought back the Raycasting and i have changed it a little bit this time and it seems to work flawlessly everytime although theirs one problem is their a way to bring back the object the raycast hits and then delete it ?
Hears the code im using
using UnityEngine;
using System.Collections;
public class GearDetectRay : MonoBehaviour {
// Use this for initialization
void Start () {
DetectGearCollided ();
}
// Update is called once per frame
void Update () {
}
void DetectGearCollided(){
Vector3 rght = transform.TransformDirection(Vector3.right);
if (Physics.Raycast(transform.position, rght, 1)) {
print("There is a Gear Right of the object!");
}
}
}
Im not sure as to what you mean. You want to destroy what the raycast hits?
You mean something like this?
void DetectGearCollided()
{
Vector3 rightDirection = transform.TransformDirection(Vector3.right);
RaycastHit hitInfo;
if(Physics.Raycast(transform.position, rightDirection, out hitInfo, 1))
{
print("There is a Gear Right of the object! Destroy it!");
Destroy(hitInfo.transform.gameObject);
}
}