Question by
Shujahat-Murtaza · Nov 03, 2016 at 12:49 AM ·
collisiondestroydamageracing
Help with Destory a vechicle code.
Hi . Developers , i am new to unity working on racing game with weapon . i have an issue on which i get stuck . I spawn a bunch of vehicles through loop , i want to destroy single vehicle if health is <=0
i spawn vehicles using this code
public void SpawnVehicles(int vehicle){
GameObject playercar;
GameObject opponentrcar;
gameTime = readyTime;
readyTimer = true;
countDown = true;
spawnPoints[0].SetActive (false);
playercar = Instantiate(playableVehicles.vehicles[raceData.vehicleNumber], spawnPoints[0].transform.position, spawnPoints[0].transform.rotation) as GameObject;
playercar.transform.rotation = spawnPoints[0].transform.rotation;
playercar.GetComponent<UnityStandardAssets.Vehicles.Car.CarUserControl>().enabled = false;
racerInfo [0].racer = playercar;
racerInfo[0].racer.name = racerInfo[0].racerName;
racerInfo [0].lap = 1;
playerController = playercar.GetComponent<UnityStandardAssets.Vehicles.Car.CarUserControl>();
racerInfo[0].aIHelper = racerInfo[0].racer.GetComponent<RG_AIHelper>();
racerInfo[0].aIController = racerInfo[0].racer.GetComponent<UnityStandardAssets.Vehicles.Car.CarAIControl>();
racerInfo [0].waypointController = racerInfo [0].racer.GetComponent<UnityStandardAssets.Utility.WaypointProgressTracker> ();
racerInfo [0].currentWaypoint = Waypoints [0];
Debug.Log (raceData.numberOfRacers[raceData.raceNumber]);
for(int i = 1; i < raceData.numberOfRacers[raceData.raceNumber]; i ++){
spawnPoints[i].SetActive (false);
opponentrcar = Instantiate(opponentVehicles.vehicles[i - 1], spawnPoints[i].transform.position, spawnPoints[i].transform.rotation) as GameObject;
opponentrcar.transform.rotation = spawnPoints[i].transform.rotation;
racerInfo[i].racer = opponentrcar;
racerInfo[i].racerName = opponentVehicles.opponentNames[i - 1];
racerInfo[i].racer.name = racerInfo[i].racerName;
racerInfo [0].lap = 1;
if(racerInfo[i].racer.GetComponent<UnityStandardAssets.Utility.WaypointProgressTracker>()){
racerInfo[i].aIHelper = racerInfo[i].racer.GetComponent<RG_AIHelper>();
racerInfo[i].aIHelper.opponentNumber = i;
racerInfo[i].aIController = racerInfo[i].racer.GetComponent<UnityStandardAssets.Vehicles.Car.CarAIControl>();
racerInfo [i].waypointController = racerInfo [i].racer.GetComponent<UnityStandardAssets.Utility.WaypointProgressTracker> ();
racerInfo [i].positionScore = 0;
racerInfo [i].currentWaypoint = Waypoints [0];
}
}
opponentrcar = null;
for (int i = 0; i < spawnPoints.Length; i++) {
spawnPoints [i].SetActive (false);
}
}
i want to destroy vehicle using java code
var hitACounter1 : int = 100;
var Target1:GameObject;
var destroyedcar:GameObject;
function Update () {
if(hitACounter1<=0){
Destroy(Target1.gameObject);
Instantiate(destroyedcar,Target1.transform.position,Target1.transform.rotation);
}
}
function OnTriggerEnter (hit : Collider) {
if(hit.gameObject.tag =="Bullet" || "Bomb"){
hitACounter1--;
}
}
This is my graduation project kindly guide me solution or any other good way to do that . Only thing left in my project is damage system .
Comment
Answer by bubzy · Nov 03, 2016 at 06:00 PM
put a script on the vehicle that manages the health and destroys itself when it reaches the criteria.