If the radius is a different size for you, that's probably because of the SphereEntity you are using, which automatically scales its transform, thereby scaling children transforms in the process.
Here is a more complete example.
private class QuarryCompanion : MonoBehaviour
{
public static void AddToEntity(BaseEntity entity)
{
var companion = entity.gameObject.AddComponent<QuarryCompanion>();
var gameObject = entity.gameObject.CreateChild();
gameObject.layer = (int)Rust.Layer.Trigger;
companion._child = gameObject;
var collider = gameObject.AddComponent<SphereCollider>();
collider.isTrigger = true;
collider.radius = 10;
gameObject.AddComponent<CollisionListener>();
}
public static void RemoveFromEntity(BaseEntity entity)
{
DestroyImmediate(entity.gameObject.GetComponent<QuarryCompanion>());
}
private GameObject _child;
private void OnDestroy()
{
DestroyImmediate(_child);
}
}
private class CollisionListener : MonoBehaviour
{
private void OnTriggerEnter(Collider collider)
{
var player = collider.ToBaseEntity().ToPlayer();
if (player == null)
return;
player.ChatMessage("You have entered the trigger");
}
private void OnTriggerExit(Collider collider)
{
var player = collider.ToBaseEntity().ToPlayer();
if (player == null)
return;
player.ChatMessage("You have exited the trigger");
}
}
private void OnServerInitialized()
{
foreach (var entity in BaseNetworkable.serverEntities)
{
var quarry = entity as MiningQuarry;
if ((object)quarry == null)
continue;
QuarryCompanion.AddToEntity(quarry);
}
}
private void Unload()
{
foreach (var entity in BaseNetworkable.serverEntities)
{
var quarry = entity as MiningQuarry;
if ((object)quarry == null)
continue;
QuarryCompanion.RemoveFromEntity(quarry);
}
}
Holy moly I love you man!! This morning I had no idea I would actually have somebody who could bring me some examples and help/information to learn more about this, this really helped a LOT, I was def. not on the right path and I thank you for the time and effort you provide to put me on the right path. All this for a trigger it is def. something to look into a little to understand this better. But faily said I am new to all this stuff, specially with Rust plugins and UnityEngine itself, but I am having a lot of fun trying all this.
I need to look into this code and try to understand everything that is going on so it becomes easier in future projects.
I had no idea about the scaling thing with my first code, will try to remember that.
I also didnt knew about DestroyImmediate()
There is a few things I do not understand instantly but I am trying to follow the code step by step to get a hang of it
Is it also possible to combine the QuarryCompanion and the CollisionListener so the OnTriggerEnter (i dont need the OnTriggerExit) can be put all into QuarryCompanion or is it really required to be seperated?
Merged postOh I didnt see you altered the message, forget my question at the end of the previous reply :P
Merged postHaha now I have 2 examples, but which on to go and use, thought the second one seems less code/confusion to me, but is it better as the first to use..? :P I cant thank you enough for your help man this is really helping me out a lot, everyday more and more I start to understand about this.
Merged postI see what you mean now as well with the GetComponentInChildren<CollisionListener>(), trying some stuff out to see what results I am getting.
From what I am getting, I seem to understand what you mean with the alternative approach, which could be indeed something to use instead, making it so the list is no longer required, saves memory too after all. Def. testing things out at the moment this code you gave me helps so much! :D
I like both approaches you gave me, and I will def. play around a little to see what fits the best but I do think the second code you gave me is a bit easier to grab on and understand what is going on. But that can also just be me :P Thanks a lot again for your help.
Merged postI used the second code you gave me, with some changes, if wanted I can post the 'final' result in here once.
Now I can continue for the next things, this was only one step but holy moly if I knew triggers had to be made this way :P but its def. still a learning progress. I hope the community I am doing this for will enjoy my work, and if not then it is what it is, at least I had fun doing this and learning this.
But hell yeah this is working def. as intended! :D 4 in the morning, time for some sleep.
Thanks again for the help.