- Home /
Having problem with killing aliens in Space Invaders.
Hi, I'm so new to coding and making a game. I'm making a Space Invaders game using Unity 2D. Everything is fine, but when I shoot a bullet by tapping the space button, the bullets are released and when they hit the aliens, they disappear, but they are unable to kill the aliens. Can you please help me with this issue?
MoveAliens script:
using UnityEngine; using UnityEngine.SceneManagement;
public class MoveAliens : MonoBehaviour { private float Speed = 2.0f; private float speedIncrease = 0.5f; public bool directionRight = true; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
if (directionRight == true)
{
transform.Translate(Vector3.right * Time.deltaTime * Speed);
}
else {
transform.Translate(Vector3.left * Time.deltaTime * Speed);
}
if(transform.position.x > 14)
{
transform.position += new Vector3(0, -1, 0);
directionRight = false;
}
if(transform.position.x < 0)
{
transform.position += new Vector3(0, -1, 0);
directionRight = true;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
SceneManager.LoadScene(0);
}
public void Die()
{
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<Collider2D>().enabled = false;
}
public void Respawn()
{
GetComponent<SpriteRenderer>().enabled = true;
GetComponent<Collider2D>().enabled = true;
}
}
RockeProjectile script:
using UnityEngine;
public class RocketProjectile : MonoBehaviour { private float speed = 5.0f;
// Start is called before the first frame update
void Start()
{
Destroy(gameObject, 3);
}
private void Update()
{
if (transform.position.y < 5)
{
transform.position += Vector3.up * Time.deltaTime * speed;
}
else
{
Destroy(gameObject);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
var enemy = collision.collider.GetComponent<MoveAliens>();
enemy?.Die();
Destroy(gameObject);
}
}
Answer by Riiich · Aug 31, 2021 at 06:06 PM
Any error codes in the debug log? Like a NullReferenceException on the Sprite renderer or Collider2D, or perhaps the bullets are hitting something else before disappearing? What about colliders, does the bullet have a single collider? Are both the bullet and alien Non-triggers, or is or both of them marked as a trigger?
There's no error in the codes, and the bullets disappear when they hit the aliens, and they move the aliens away for a little, which means that collision is happening, and the main problem is with the death of aliens. For both the aliens and the rocket and the bullets I'm using polygon collider. About the triggers, I'm not sure, what form they should be in?? @riiickn
@lakkiarash could you take a screenshot of the inspector, while you've selected the alien? :) Just want to check the settings. Also, does the bullet or alien have a rigidbody attached?
@Riiich I tried to upload a picture, but I couldn't. The bullet has a rigidbody attached. Our teacher said that I should tag the aliens as "enemy" in the inspector, and in my script make a change for GetComponenet part, and type "enemy" instead. I thought that the problem is with the die function, because as my bullets hit the aliens, they disappear and the collision function is working, the only problem is that aliens don't die (disappear).