- Home /
Problem with collider, rigidbody 2D
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.
Your answer
Follow this Question
Related Questions
Static Colliders in the 2D Engine in Unity 5 1 Answer
Moving colliders that are part of a composite collider 1 Answer
Making 2D objects solid, so that they do not pass through eachother? 1 Answer
How not to lose speed when collided with wall? 2 Answers
Is it possible to have continuous collision detection with dynamic points on Edge Colliders? 0 Answers