using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shooting : MonoBehaviour
{
public GameObject player;
public Transform firepoint;
public GameObject bulletPrefab;
public GameObject bullet;
public float bulletforce = 100f;
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoint.up * bulletforce, ForceMode2D.Impulse);
}
void Start()
{
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), bullet.GetComponent<Collider2D>());
}
}
I want to make the bullet will not Collison with other bullet
// Physics2D.IgnoreCollision(bullet.GetComponent(), bullet.GetComponent());
but this does not work…
You’ve got a public “GameObject bullet” but what is this set to when it starts? Nothing? If so, I’m surprise you don’t get any errors because immediately upon start you then try to get it to ignore the first collider on it with the same collider i.e. itself. That makes no sense.
You have a private “Shoot()” method that is called anytime you fire but it replaces your public “bullet” field. It looks like you don’t need that public field at all.
May I ask if you are new to Unity/scripting? I’m assuming you are so forgive me if the following is stuff you already know.
If you don’t want “bullets” to contact other “bullets” then you should create a “Bullet” layer. Assign the bullet prefab to that layer. Then you can go into “Menu > Edit > Project Settings > Physics 2D”. There you’ll see a “Layer Collision Matrix”. Ensure that the row/column where your new “Bullet” layer intersect is unchecked. This tells 2D physics that these should ignore each other.
The “IgnoreCollision” doesn’t need to be used here. This is for when you need to specify two specific colliders that should ignore each other. By the sound of it, you don’t want this. You want all bullets to ignore each other."
Here’s a good overview of 2D physics.
Hope this helps.
1 Like
Thx, i get it. I am new to unity:smile:
1 Like