HELP - Setting up my Collider "while" adding it

So I am trying to set up my collider’s Center and Size:

WHY?
imagen
They’re missing its original size and possition settings, so they appear far away, at a wrong size
imagen

Ok how am i trying to set it?

 private void Start()
    {
        AddNonTriggerBoxColliderCollider();
    }

    private void AddNonTriggerBoxColliderCollider()
    {
        Collider myBoxCollider = gameObject.AddComponent<BoxCollider>();
        myBoxCollider.isTrigger = true;
        //myBoxCollider.center = new Vector3(-35.8, 2.14, -43.1);
        //myBoxCollider.size = (10.5, 1.57, 8.27);
    }

edit: This is how it looks in Visual Studio when the compiler complains:


How should I be doing it instead?

I’ve been investigating online but people seem to do a GetComponent after adding it… but it just doesn’t feel right, I would like to create the collider with the right settings. Is that possible? Or what would be the most correct way to do this?

Evenin Manu, just popping this in during T break at work, its a bit short im afraid, but Hope it gets you on track…

you could cast it as a BoxCollider Type, giving you access to what you see in the inspector.
Collider type itself doesnt contain Center or Size straight off the bat, you could use the Collider Bounds property.

Try changing your first line to a BoxCollider and remember the f for floats too. otherwise it things the values are doubles :frowning:

    private void AddNonTriggerBoxColliderCollider()
    {
        BoxCollider myBoxCollider = gameObject.AddComponent<BoxCollider>();
        myBoxCollider.isTrigger = true;
        myBoxCollider.center = new Vector3(-35.8f, 2.14f, -43.1f);
        myBoxCollider.size = new Vector3(10.5f, 1.57f, 8.27f);
    }

just a quick question, im guessing that you want the collider to be that far away from the centre of the object the collider is attached to? just that using that would be relative coordinate values from the objects local transform.

if you want the centre to be at the centre of the object its attached to Vector3.Zero, or just not setting the center line should do it.

Hey OboShape, thanks for the reply,

Making it BoxCollider somehow slows the game a lot, but also won’t collide either.

Trying to answer your question, which I am not sure I understood:
I want the collider to spawn there because somehow that happens to appear like the center of the ship, rather than (0,0,0). But I most likely need to re-arange the ship parts because this amount of offsetting is starting to be too much of a mess.

edit: Looks like the BoxCollider doesn’t slow the game anymore (???)
Also, the Collider seems to be in the correct place as you can see in the picture.

But still, won’t collide as the other one (The deactivated one) used to work.

Just a side note, you have the newly added one set as a Trigger, you didn’t have that on the disabled one. You’d need to use OnTriggerEnter as opposed to OnCollisionEnter to detect the message.

1 Like

was going to mention that one too Rob, nice spot.

on another note, and this one could go down a bit of a rabbit hole, so bear with it.

I apologise if I have the wrong end of the stick, but, it would appear that you have ‘one X wing’ as an enemy type, as the parent in the heirarchy and has the box colliders attached.
you also appear to have a child object that, im assuming contains the mesh of the model.

just to save you some headaches down the road, is there any reason that you couldnt create multiple box colliders, much like you have showing there, and resize them to the appropriate mesh size. and then set the whole thing as a prefab.

that way you could change something once with it, and it would propogate through every instance if it.

Like i say, maybe im getting it wrong, but i dont think that creating the box colliders on the fly is the way to go in this case, if its a case of instantiating that enemy alot.

now lets visit the collisions, could we see the collision detection code please. and im assuming that one of the objects that is within the collision check has a Rigidbody attached?

1 Like

Fully agree - this stems from a part of the Argon Assault course where this was done, I don’t really even know why to be honest - most likely to show that you can add components on the fly - it wasn’t really necessary then either, in my opinion.

1 Like

ah i do apologise, ive only reached the adding enemies section :frowning:
didnt know Rick had that in there, i would imagine thats why it was there just to show it can be done.

1 Like

No need to apologise Daz, I think this bit was a bit of Ben’s as opposed to Rick’s but yeah, more of a case of “hey, you can do this at runtime” over “hey, you should do this at runtime”.

I’d opt for your suggestion here, create the GameObject with all the required bits, then prefab it, instantiate as required. Hope all is well btw :slight_smile:


Updated Wed Nov 21 2018 19:46

Just for reference, it’s in Lecture 101 : Making Scripts Add Components, here’s the learning objectives from that lecture;

27 Making Scripts Add Components

In this video (objectives)…

  1. Remove colliders from asset pack prefabs, returning to vanilla state.
  2. Arrange for enemies to add their own colliders on start.

After watching (learning outcomes)…
Arrange for game objects to add their own required components at run time, reducing their dependency on assets being setup a particular way.

It looks like it was primarily down to the configuration of the imported assets but I still think this was probably more of a case of showing you how to add components at runtime - they could have just as easily been setup before creating new enemy prefabs. Hope this is of use :slight_smile:

Thanks for the replies guys, I try to reply in order to the key points:

Rob 22h
Just a side note, you have the newly added one set as a Trigger , you didn’t have that on the disabled one. You’d need to use OnTriggerEnter as opposed to OnCollisionEnter to detect the message.

Ohh I see!

OboShape Lifetime Member
could we see the collision detection code please.

&


Rob22h
Just a side note, you have the newly added one set as a Trigger , you didn’t have that on the disabled one. You’d need to use OnTriggerEnter as opposed to OnCollisionEnter to detect the message.

I am checking for “OnParticleCollision”, guess that’s why it works with a Collider without trigger. I can confirm that it works without trigger.

Guess I will stash the knowledge and keep colliders as an “editor component” addition.

Anyway it’s been an interesting exercise as I now realize the word “new” is there to be understood too.

Thanks!

1 Like

You’re very welcome Manu :slight_smile:

Privacy & Terms