Hello
I’m writing to you in order to get help, because im beginner, and i’m stuck. I have 2 objects - bullet and asteroid, and my goal is that fired bullets has to destroy asteroid, but when i enter play mode bullets are just going through it and don’t interact.
To the asteroid i added components rigidbody2d set to kinematic and polygon collider 2d, and for bullet i added rigidbody2d component set to kinematic
Below I’m showing codes for both classes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Collider2D))]
public class Asteroid : MonoBehaviour
{
[SerializeField]
float Durability = 6f;
void Start()
{
SetSpeed();
}
void Update()
{
}
private void SetSpeed()
{
var targetSpeed = Random.Range(2f, 3f);
GetComponent<Rigidbody2D>().velocity = Vector2.down * targetSpeed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
var obj = collision.gameObject;
var bullet = obj.GetComponent<Bullet>();
if (bullet != null)
{
Durability--;
Destroy(obj);
if (Durability <= 0)
Destroy(gameObject);
}
}
}
===============================================================================
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum CannonType { Single, Double, Triple }
[System.Serializable]
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(SpriteRenderer))]
public class Bullet : MonoBehaviour
{
[SerializeField]
float Speed = 6f;
public void Configure(BulletType bulletType)
{
GetComponent<SpriteRenderer>().sprite = bulletType.Sprite;
GetComponent<Rigidbody2D>().velocity = transform.rotation * Vector3.up * bulletType.Speed;
}
i’m sorry to bother, and kindly asking for help.