• 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 /
  • Help Room /
avatar image
0
Question by alx6her · Mar 03, 2016 at 10:39 PM · gameobject2d game2d-platformer2d-physicsstealth

2D Character Invisible To Enemies

I am working on a stealth side scrolling game for my comp. sci class and I'm somewhat of a newbie in terms of programming in Unity, so far I have the game set up so that the enemy walks in a range but I have trouble making the player character invisible when he presses the up button in order to "hide" from enemies.

So the enemy should pass through when the player is invisible (upon pressing up, of course), and if the player is caught when they are not invisible, then the game is over.

The tutorials on the site are plenty helpful but they don't really cover things like basic side scrolling games with no health system in place or stealth mechanics in a 2D game. I wasn't sure if I had to make my player character a collider trigger or whatever it is called in order to have the enemies pass through him or what.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zoogyburger · Mar 04, 2016 at 01:36 AM

Here's an example

 public BoxCollider2D mybounds;
 
 void Start()
     {
         mybounds = GetComponent<BoxCollider2D> ();
     }
 
     void Update ()
     {
 
         if (Input.GetKeyDown (KeyCode.Space)) {
             mybounds.isTrigger = true;
         }
         if (Input.GetKeyDown(KeyCode.Backspace)){
             mybounds.isTrigger = false;
         }
     
     }
 
     void OnCollisionEnter2D (Collision2D other)
     {
         if (other.gameObject.tag == "Enemy") {
             Destroy (gameObject);
 
     }
         
     }
     
 }

Make sure your player has a boxcollider2D and your enemies are tagged "Enemy".

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 alx6her · Mar 04, 2016 at 02:14 AM 0
Share

The OnCollisionEnter2D destroys the player character, correct?

EDIT: Although it didn't really do that, the two characters just continue bumping into one another.

I have it set to:

void OnCollisionEnter2D (Collision2D other) { if (other.gameObject.tag == "enemy1") { Destroy (gameObject); } }

avatar image alx6her · Mar 04, 2016 at 02:23 AM 0
Share

The player character also just falls through the stage when the button is pressed as well, does making it a trigger cause that to happen and if so, what can one do to prevent that from happening?

avatar image Zoogyburger · Mar 04, 2016 at 03:58 AM 1
Share

Change the

 if (other.gameObject.tag == "enemy1") {
      Destroy (gameObject); } }

To

 if (other.gameObject.tag == "Player") {
     Destroy (other.gameObject);

And put this script on the Enemy with the $$anonymous$$ybounds in the inspector set to the player and make sure your player is tagged Player. If the Player is falling through the floor in his Rigidbody2D change the Gravity scale to be 0.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

59 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

Related Questions

How to create a 2D obstacle as a question 0 Answers

2D Platformer Projectile wont face mouse position? 0 Answers

I just want to understand why when i check Is Trigger on my 2D player he dont collide with any layer but my trigger works fine instead of when i uncheck Is Trigger he collided and works i really want to to knew 1 Answer

Player Not Moving With Platform 2D 1 Answer

2d platformer physics 0 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