Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

Built game doesn't work as the game preview 0 Answers

Using AddForce for horizontal movement doesn't quite work the way I want it to (2D) 1 Answer

Can you improve my animation/movement code? *Bugs in details 1 Answer

2D Dynamic Lights and Shadows 0 Answers

Ragdoll Movement Script, Help! 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