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.
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.
using UnityEngine;
using System.Collections;
public class CarModifier : MonoBehaviour
{
//mouse info
public Ray mouseray;
public float mouse_raydistance = 50f;
public RaycastHit mouse_raycasthit;
public Vector3 mouseforward;
//
public string vehiclenode_bumperstring_rear = "RearBumper";//Bumper tag
public string vehiclenode_bumperstring_front = "FrontBumper";//Bumper tag
public string vehiclenode_w1string = "Wheel1";//Wheel1 tag
public string vehiclenode_w2string = "Wheel2";//Wheel2 tag
public string vehiclenode_w3string = "Wheel3";//Wheel3 tag
public string vehiclenode_w4string = "Wheel4";//Wheel4 tag
public string vehiclenode_hoodstring = "Hood";//Hood tag
public bool isNodeSelected;//has a node been selected?
public Transform current_node;//current node that player has clicked.
public Transform rearbumper_prefab;
public Transform rearbumper_node;
public Transform rearbumper_vehiclenode;
public Vector3 rearbumper_prefab_vec;
public Vector3 rearbumper_node_vec;
public Vector3 rearbumper_vehiclenode_vec;
public Vector3 selectednode_vec;
public enum vehiclenode_type
{
Bumper,
Wheel,
Hood
}
void Start ()
{
mouseforward = Vector3.forward;
mouseray = Camera.main.ScreenPointToRay (Input.mousePosition);
current_node = null;//player hasnt clicked a node
selectednode_vec = current_node.position;
rearbumper_prefab_vec = rearbumper_prefab.localPosition;
rearbumper_node_vec = rearbumper_node.localPosition;
rearbumper_vehiclenode_vec = rearbumper_vehiclenode.localPosition;
}
void Update ()
{
MouseManipulator ();
}
void MouseManipulator ()
{
if (Input.GetButtonDown ("0")) {//left click
if (Physics.Raycast (mouseray, mouse_raycasthit, mouse_raydistance)) {
string mouse_raycasthit_namestring = mouse_raycasthit.transform.name;
string mouse_raycasthit_tagstring = mouse_raycasthit.transform.tag;
Debug.Log ("Hit this" + mouse_raycasthit_namestring + " | Hit Distance " + mouse_raycasthit.distance);
Debug.Log (mouse_raycasthit_tagstring);
if (mouse_raycasthit.transform.tag = vehiclenode_bumperstring_front) {
current_node = mouse_raycasthit.transform;
isNodeSelected = true;//mostly use for debugging
VehicleNodeManipulator ();//trip this method and handle what you want to do with the node here.
} else {
//must be another tag
}
} else {
//either hit nothing or its' too far away.
}
} else {
//must of pressed another button
if (Input.GetButtonDown ("1") && isNodeSelected == true) {//right click
ClearSelectedNode ();
}
}
}
//These methods are children of the mouse manipulator method. This method is still in control of "Update();
void ClearSelectedNode ()
{//clear the selected item to allow for another selected item?
current_node = null;
isNodeSelected = false;//mostly use for debugging
}
//These methods are children of the mouse manipulator method. This method is still in control of "Update();
void VehicleNodeManipulator ()
{//trip this method and handle what you want to do with the node here.
Instantiate (rearbumper_prefab, current_node.localPosition, current_node.rotation);
}
}
EDIT: One thing I can’t help you with is how to save the vehicles once you change the parts.
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.
Thanks!
Yep you’re welcome. Hopefully it will get you moving in the right direction.