Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lakkiarash · Aug 31, 2021 at 01:07 PM · unity 2dcollision detectionshootingshooterspace invaders

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lakkiarash · Aug 31, 2021 at 05:44 PM

Can anyone help me please?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lakkiarash · Aug 31, 2021 at 10:42 PM 0
Share

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

avatar image Riiich lakkiarash · Aug 31, 2021 at 11:43 PM 0
Share

@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?

avatar image lakkiarash · Sep 01, 2021 at 02:04 AM 0
Share

@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).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why doesn't AddForce work upwards? 0 Answers

"the object of type transform has been destroyed but you are still trying to access it" problem, 1 Answer

Optimizing Code 1 Answer

FPS rigidbody bullets not moving to center of screen 0 Answers

Enemy fire rate? help! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges