- Home /
How would I swap out a clean car body with a damaged car body but only in the area the car got hit?
I'm trying to find out how to complete this task for my final major project in my final year of university. I'm a Games artist by trade so I'm not super familiar with code although I am trying to learn as much as i can. any theory's are welcome.
Answer by A_Lego · Oct 27, 2018 at 03:54 AM
If you mean, for example, when the car gets hit on the rear it damages the rear bumper, then this isn't too difficult. I would simply set different colliders to each body part that you would like to be damagable, then create a script using the "OnCollisionEnter" function to detect for the hit itself. Once the hit is registered, you can just deactivate the "clean car body part" and active the damaged version of it. You can also set conditions to check if the car's(Or the object that hit the car) velocity is above a certain threshold to create a slightly more realistic effect(This also prevents your car from being destroyed when you just get a little scratch). //An array of GameObjects with body the "clean" body part and damaged version public GameObject[] BodyParts;
void Start(){
//Sets the clean version to be active
BodyParts[0].SetActive(true);
//and the damaged version to not active
Bodyparts[1].SetActive(false);
}
void OnCollisionEnter(Collision other){
//Checks to make sure that the body part doesn't run into itself or other parts of the car and checks its velocity to make sure it isn't stationary
if(other.tag != "CarParts" && GetComponent<Rigidbody>().velocity != Vector3.zero){
if(BodyParts[0].activeSelf){
BodyParts[0].SetActive(false);
BodyParts[1].SetActive(true);
}
}
}
It's not perfect, but it'll get you headed in the right direction
Thank you so much for your help and speedy reply. That helps out a lot