Merhaba böyle birşey yaptım mermi düşmana değince merminin yok olmasını istiyorum ama olmuyor düşmanın içinden geçiyor amacım şey düşmana deydiği anda merminin yok olmasını istiyorum destroyu yanlışmı yaptım
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
private Animator anim;
public Rigidbody2D rb;
public float speed;
Vector2 move;
public Camera cam;
public GameObject bullet;
public float bulletspeed;
public Transform gun;
public Image canimage;
private float Can = 150f;
public Text puantext;
public int puansayısı;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
rb.MovePosition(rb.position + move * speed * Time.deltaTime);
Vector2 mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
Vector2 dir = mousePosition - rb.position;
float dir2 = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
rb.rotation = dir2;
if (Input.GetKeyDown(KeyCode.G))
{
GameObject bullet2 = Instantiate(bullet, gun.position, Quaternion.identity);
Rigidbody2D rb = bullet2.GetComponent<Rigidbody2D>();
rb.AddForce(gun.up * bulletspeed);
Destroy(bullet2.gameObject, 1.6f);
}
if (Input.touchCount > 0)
{
Touch finger = Input.GetTouch(0);
if (finger.phase == TouchPhase.Began)
{
GameObject bullet2 = Instantiate(bullet, gun.position, Quaternion.identity);
Rigidbody2D rb = bullet2.GetComponent<Rigidbody2D>();
rb.AddForce(gun.up * bulletspeed);
Destroy(bullet2.gameObject, 1.6f);
}
else
{
anim.SetBool("tosaldırı", false);
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("ölme");
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("zombi"))
{
Can -= 10;
canimage.fillAmount = Can / 150f;
float x = Can / 150f;
canimage.color = Color.Lerp(Color.red, Color.green, x);
}
}
public void puan(int p)
{
puansayısı += p;
puantext.text = "" + puansayısı;
}
}buda düşman kodu
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
public class zombie : MonoBehaviour
{
private Animator anim;
public Transform target;
public int health = 1;
private Player p;
public int zombipuanı = 10;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
p = GameObject.Find("Tower").GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position, target.position) > 1.5)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, 2 * Time.deltaTime);
}
else
{
anim.SetTrigger("saldırı");
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("mermi"))
{
health--;
if (health == 0)
{
p.puan(zombipuanı);
anim.SetTrigger("ölme");
Destroy(this.gameObject, 2f);
}
}
}
}