Search Unity

Multiple models with colliders.

Discussion in 'General Discussion' started by tclancey, May 26, 2020.

  1. tclancey

    tclancey

    Joined:
    May 19, 2017
    Posts:
    143
    Hi folks.

    To get used to the basics and middle ground of unity, of course I've decided to write a simple 2.5d racer. Everyone has one tucked away in a backup somewhere I'm sure. It's also a subject so well battered into the ground there are loads of tubes which is really helpful.

    But I have a requirement I can't find covered anywhere. This is completely over the top for this particular game, but will be needed for my first 'proper' job.

    For example I'll create 5 cars. All different shapes and sizes, all different wheel placements. I'm creating the models in Blender and will name all the model parts as required. When I drop a car into the scene it will have it's body and 4 wheels, I'll create a rigid body and box collider (for now), I'll also add any public variables the next step may need. I need unity (on run) to create a wheel collider for each wheel, setting scale, location, blah blah.

    However, imagine there is a car, a bike, a tank, an 18 wheel truck. Every wheel will need an attribute for left/right, power, steer, lean, and probably lots of other stuff.

    Now, I know how to search the car body's children, more or less, it's creating the colliders, moving stuff around etc I really need some pointers with.

    Please suggest some links or advice if you can.
    Many thanks.
     
  2. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Each visual component needs a logic component togo with it. You can then choose to serilize this into the asset or at runtime get them with code
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    What's the question?

    You can automate all of it, if you want, or reduce it to one click operation. Adding collider, rigidbody, and so on is a call to AddComponent. If you follow a certain naming scheme, then you can make a script which will iterate through objects in your imported model, and add "Wheel" components to objects named "wheel", recognize car body as something that needs "car" component, and then based on body orientation and relative object position it could determine which wheel is which and even create wheel colliders to them. However, this is likely beyond the scope of whaty ou're trying to do.

    So. What's the question? What are you trying to do and don't know how to do?
     
    Joe-Censored and angrypenguin like this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Why do you specifically need Unity to do it "on run"?

    The standard workflow is for you to import your model and then add Components as necessary to define the behaviour you want. Reused objects can be saved as a Prefab.

    You can code up custom functionality by making your own Components, which are MonoBehaviours. You'll likely need that to at least implement your game's controls and rules.
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can add components such as colliders at runtime, but I'm wondering where are these wheels you are adding coming from? You could easily just have the proper components already attached to the wheel as a prefab, and just do a bit of configuration when you add them to the vehicle. I'm just guessing since I don't exactly understand your use case.
     
  6. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    I like to be able to choose between convention or declarative approach so I often do

    Code (CSharp):
    1. MyMember = MyMember != null ? MyMember : GetComponent<MyComponent> ();