Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vehicle Customization methods

Discussion in 'Scripting' started by darryldonohoe, Jan 9, 2016.

  1. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Hey guys,

    I don't know if you could help me with something. I created a very simple customization script.
    It will be used for a vehicle where certain body parts can be replaced. The way it is working now is:

    Empty GO with a few empty children objects attached, these objects represents snap-points".
    The snap point will be used for placing the right body part at the right position.

    At startup, an Initializing method will be called, searching through the snappoint list to see if the snappoint already has an attached body part.

    All is working pretty good, however as it is set up right now. All the snappoints have a "locked" position.
    For example:
    If i would like to replace the body with a wider and longer model, the snappoints for the wheels would still be in the old position.

    So i guess the question is, do you have an idea how to set this up?

    I thought of this;

    Empty GO, with 1 empty child object (snappoint will be used for the body)
    The body will have a few empty children objects as well (instead of the main Empty GO)
    Whenever i replace the body, i call a method and edit every already placed bodypart so it matches the new snappoints.
    By looking for new snappoints using: GameObject.Find("wheels") "turret" etc...

    However, this would lead to constantly changing already placed body parts (4 different types) once the body is switched.

    Does this idea sounds good? Or do you have any advice on how to tackle this?

    Right now the code looks like:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5.  
    6. public class test : MonoBehaviour
    7. {
    8.     //list of snappoints where the body parts will be placed
    9.     public List<SnapPoint> snappoints = new List<SnapPoint>();
    10.     //list of body parts
    11.     public List<BodyPart> bodyParts = new List<BodyPart>();
    12.    
    13.     void Start()
    14.     {
    15.         Initialize();
    16.     }
    17.  
    18.     //the Initialize method will load our saved load-out, going through each snappoint to check if it is occupied, if it is
    19.     //call the ChangePart method passing through the snappoint attached body part ID
    20.     public void Initialize()
    21.     {
    22.         foreach (SnapPoint snap in snappoints)
    23.         {
    24.             if (snap.occupied == 1)
    25.             {
    26.                 ChangePart(snap.partID);
    27.             }
    28.         }
    29.     }
    30.  
    31.     //the ChangePart method will handle placing/changing a body part, with the body part's ID we search our list of bodyparts
    32.     //instantiate the right one, get the slotType value, if the snappoint has an already attached body part, we destroy it
    33.     //and replace it with the new instantiated body part
    34.     public void ChangePart(int partID)
    35.     {
    36.         GameObject curPart = Instantiate(bodyParts[partID].model) as GameObject;
    37.  
    38.         int thisID = bodyParts[partID].slotType;
    39.      
    40.         if(snappoints[thisID].currentPart != null)
    41.         {
    42.             Destroy(snappoints[thisID].currentPart);
    43.         }
    44.  
    45.         curPart.transform.position = snappoints[thisID].slot.transform.position;
    46.         curPart.transform.parent = snappoints[thisID].slot.transform;
    47.         snappoints[thisID].currentPart = curPart;
    48.     }
    49. }
    50.  
    51.  
    52. [Serializable]
    53. public class SnapPoint
    54. {
    55.     //snappoint's name, not necessary but useful when viewing the list in the inspector
    56.     public string name;
    57.     //our snappoint gameObject, to access the position and rotation
    58.     public GameObject slot;
    59.     //what current body part is attached to this snappoint
    60.     public GameObject currentPart;
    61.     //if the snappoint has an attached bodypart, we can use this for the initialization, since we can not check if it has a
    62.     //GameObject attached in the beginning
    63.     public int occupied;                 //0=false, 1=true
    64.     //serialization/deserialization
    65.     public int partID;
    66. }
    67.  
    68. [Serializable]
    69. public class BodyPart
    70. {
    71.     //body part name we can show in our "garage/shop" UI
    72.     public string name;
    73.     //the model that represents this body part
    74.     public GameObject model;
    75.     //maybe useful?
    76.     public int id;
    77.     //the slot type tells us where this body part should go, e.g: 0 = upper body , 1 = wheels etc...
    78.     public int slotType;
    79.     //not being used at the moment, but will be used for how much gold we can buy this body part
    80.     public int gold;
    81. }
    82.  
    I also want to save and load these customizations. (i already have a working saving loading script)
    But for that to work, i need an Initialize method to read which part was equipped.

    Anyway hope you guys can help me out.

    Thanks in advance,

    Darryl