Hello, I am making a game in which you are a ship that spins around in order to shoot approaching enemies.
I have two prefabs that i instantiate in this game; the enemy and the bullet.
I have no trouble instantiating each prefab, and no trouble with their movement, but what I am having trouble with instead is collision detection.
What I want is for the bullet to log something to the console(once I get this I will be fine on my own) but it just doesn’t seem to work.
public class shoot : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnCollisionEnter2D()
{
Debug.Log("hello");
}
// Update is called once per frame
void Update () {
Transform transform = this.transform;
Vector2 position = transform.position;
Vector3 rotation = transform.rotation.eulerAngles;
float EulerAngle = rotation.z;
EulerAngle /= 180;
EulerAngle *= (float)Math.PI;
position.x += (float)Math.Cos(EulerAngle) / 30;
position.y += (float)Math.Sin(EulerAngle) / 30;
this.transform.position = position;
StartCoroutine("update");
}
IEnumerator update()
{
yield return new WaitForFixedUpdate();
if (Math.Abs(this.transform.position.x)<10)
{
Update();
}
else
{
Destroy(this.gameObject);
}
}
}
This is the script for the bullet that is instantiated, and the only thing that isn’t working is the “OnCollisionEnter” near the top.
To be clear, I have a 2d circle collider and rigidbody on both the bullet and enemy.
Why doesn’t my code log “hello”?