Search Unity

Solution for car customization

Discussion in 'General Discussion' started by blastintyres, Dec 19, 2014.

  1. blastintyres

    blastintyres

    Joined:
    Nov 19, 2014
    Posts:
    79
    I am making a car customization menu.
    In which the user will have the ability change the rear and front bumpers of the cars.
    The amount of cars is above hundred,so instead of modeling 100 bumpers for all the cars.,.
    Is there a way to script one bumper to fit all the hundred cars,including SUV's which are more higher and wider.
     
  2. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    Sure, theres possibly infinite amount of ways to do this with both 2D and 3D. This technique can work with all other vehicle parts. First I would start with shattering the vehicle's model or 2D image and having everything major on a node. Every vehicle will need to have this.

    Images from my current project.





    Pre Shatter:

    Shattered Bumper:

    Shattering your vehicle and putting everything major on nodes will allow you use these nodes as empty transforms and you can snap things to them. You can see in the image how i put the bumper on its' own node.

    Code wise is another story. This is where it will get difficult in my opinion. While in unity you will need to make sure the nodes can be seen by the player and can be clickable. I personally recommend making it so the nodes show up as spheres with colliders.

    This is just the base line setup for the node manipulator. There is so much more that can be done.

    As you can see that I didn't use the enums. You can fill the enums to do a switch case between each item. Make it so that if enum == this then either allow it or remove it due to it being the wrong piece. You wouldn't put a bumper as a tire or a door as a wheel.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CarModifier : MonoBehaviour
    6. {
    7.         //mouse info
    8.         public Ray mouseray;
    9.         public float mouse_raydistance = 50f;
    10.         public RaycastHit mouse_raycasthit;
    11.         public Vector3 mouseforward;
    12.         //
    13.         public string vehiclenode_bumperstring_rear = "RearBumper";//Bumper tag
    14.         public string vehiclenode_bumperstring_front = "FrontBumper";//Bumper tag
    15.         public string vehiclenode_w1string = "Wheel1";//Wheel1 tag
    16.         public string vehiclenode_w2string = "Wheel2";//Wheel2 tag
    17.         public string vehiclenode_w3string = "Wheel3";//Wheel3 tag
    18.         public string vehiclenode_w4string = "Wheel4";//Wheel4 tag
    19.         public string vehiclenode_hoodstring = "Hood";//Hood tag
    20.         public bool isNodeSelected;//has a node been selected?
    21.         public Transform current_node;//current node that player has clicked.
    22.         public Transform rearbumper_prefab;
    23.         public Transform rearbumper_node;
    24.         public Transform rearbumper_vehiclenode;
    25.         public Vector3 rearbumper_prefab_vec;
    26.         public Vector3 rearbumper_node_vec;
    27.         public Vector3 rearbumper_vehiclenode_vec;
    28.         public Vector3 selectednode_vec;
    29.  
    30.         public enum vehiclenode_type
    31.         {
    32.                 Bumper,
    33.                 Wheel,
    34.                 Hood
    35.         }
    36.  
    37.         void Start ()
    38.         {
    39.                 mouseforward = Vector3.forward;
    40.                 mouseray = Camera.main.ScreenPointToRay (Input.mousePosition);
    41.                 current_node = null;//player hasnt clicked a node
    42.                 selectednode_vec = current_node.position;
    43.                 rearbumper_prefab_vec = rearbumper_prefab.localPosition;
    44.                 rearbumper_node_vec = rearbumper_node.localPosition;
    45.                 rearbumper_vehiclenode_vec = rearbumper_vehiclenode.localPosition;
    46.         }
    47.  
    48.         void Update ()
    49.         {
    50.                 MouseManipulator ();
    51.         }
    52.  
    53.         void MouseManipulator ()
    54.         {
    55.                 if (Input.GetButtonDown ("0")) {//left click
    56.                         if (Physics.Raycast (mouseray, mouse_raycasthit, mouse_raydistance)) {
    57.                                 string mouse_raycasthit_namestring = mouse_raycasthit.transform.name;
    58.                                 string mouse_raycasthit_tagstring = mouse_raycasthit.transform.tag;
    59.                                 Debug.Log ("Hit this" + mouse_raycasthit_namestring + " | Hit Distance " + mouse_raycasthit.distance);
    60.                                 Debug.Log (mouse_raycasthit_tagstring);
    61.                                 if (mouse_raycasthit.transform.tag = vehiclenode_bumperstring_front) {
    62.                                         current_node = mouse_raycasthit.transform;
    63.                                         isNodeSelected = true;//mostly use for debugging
    64.                                         VehicleNodeManipulator ();//trip this method and handle what you want to do with the node here.
    65.                                 } else {
    66.                                         //must be another tag
    67.                                 }
    68.                         } else {
    69.                                 //either hit nothing or its' too far away.
    70.          
    71.                         }
    72.                 } else {
    73.                         //must of pressed another button
    74.                         if (Input.GetButtonDown ("1") && isNodeSelected == true) {//right click
    75.                                 ClearSelectedNode ();
    76.                         }
    77.                 }
    78.         }
    79.  
    80. //These methods are children of the mouse manipulator method. This method is still in control of "Update();
    81.         void ClearSelectedNode ()
    82.         {//clear the selected item to allow for another selected item?
    83.                 current_node = null;
    84.                 isNodeSelected = false;//mostly use for debugging
    85.         }
    86. //These methods are children of the mouse manipulator method. This method is still in control of "Update();
    87.         void VehicleNodeManipulator ()
    88.         {//trip this method and handle what you want to do with the node here.
    89.                 Instantiate (rearbumper_prefab, current_node.localPosition, current_node.rotation);
    90.         }
    91. }
    92.  
    EDIT: One thing I can't help you with is how to save the vehicles once you change the parts.
     
    Last edited: Dec 19, 2014
  3. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    I hate to get mildly off subject but how would someone go along with trying to save a vehicle or a model after being customized? As far as I know the smart way would be to just create a new prefab at runtime but I don't think Unity3D is capable of doing this.
     
  4. blastintyres

    blastintyres

    Joined:
    Nov 19, 2014
    Posts:
    79
    Thanks!
     
    wbakunis likes this.
  5. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    Yep you're welcome. Hopefully it will get you moving in the right direction.