Question: Unity C# The following code implements several functions: Two tanks can shoot at each other and will destroy each other (but cannot respawn) Requirements: When
Unity C#
The following code implements several functions: Two tanks can shoot at each other and will destroy each other (but cannot respawn)
Requirements: When any tank is destroyed by the opponent's bullet, it will respawn at the default location after a few seconds delay. (Unlimited rebirth)
code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
GameObject tank1;
GameObject tank2;
public int bullet;
// Start is called before the first frame update
void Awake()
{
tank1 = GameObject.Find("Tank1");
tank2 = GameObject.Find("Tank2");
}
void OnTriggerEnter2D(Collider2D other)
{
if (bullet == 1)
{
if (other.tag == "Wall")
{
Destroy(this.gameObject);
}
if (other.tag == "Tank2")
{
Destroy(this.gameObject);
Destroy(other.gameObject);
//tank2 = Instantiate(tank2, transform.position, transform.rotation);
}
}
else if (bullet == 2)
{
if (other.tag == "Wall")
{
Destroy(this.gameObject);
}
if (other.tag == "Tank1")
{
Destroy(this.gameObject);
Destroy(other.gameObject);
//tank1 = Instantiate(tank1, transform.position, transform.rotation);
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
