Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 anjobee · Aug 27, 2019 at 04:09 AM · collision2d-platformercollidersphysics2d2d-physics

I want my player to walk through walls

I am making a 2d platformer game and I need help on coding this script that makes the player walk through walls when a key is pressed and not lose gravity + do not fall through the platform.

// Start is called before the first frame update void Start() { rigidBody = GetComponent(); bodyCollider = GetComponent(); }

 // Update is called once per frame
 void FixedUpdate()
 {
     if (Input.GetButton("Phase"))
         {
         rigidBody.gravityScale = 1;
         bodyCollider.isTrigger = true;
         }
         else
         {
         rigidBody.gravityScale = 1;
         bodyCollider.isTrigger = false;
     }
 }
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
0
Best Answer

Answer by I_Am_Err00r · Aug 27, 2019 at 01:49 PM

I'm going to write you a seperate script that will do exactly what you want, but this really should be attatched to something like a player controller script or the script that translates input.


In this example, you need to attach this directly to the player, set all the walls to a tag (for the example I provided, it will be "Walkthrough") and the key you need to press will be "L" (only because I would assume it doesn't conflict with any other button you might have mapped, you would need to set that to something else if you don't want the player to press and hold "L" and on release it will make the walls solid again)

 using UnityEngine;
 using System.Collections.Generic;
 
 public class WallScript : MonoBehaviour
 {
     List<GameObject> walls = new List<GameObject>();
     
     private void Start()
     {
         walls = GameObject.FindGameObjectsWithTag("Walkthrough");
     }
     
     private void Update()
     {
         WalkthroughWalls();
     }
     
     private void WalkthroughWalls()
     {
         foreach(Collider2D col in walls)
         {
             if(Input.GetKeyUp(KeyCode.L)
             {
                 col.enabled = true;
             }
             else if(Input.GetKey(KeyCode.L)
             {
                 if(col.enabled = false)
                 {
                     return;
                 }
                 col.enabled = false;
             }
         }
     }
 }

I haven't tested that with Unity, and am not such a wiz at coding that I might have not have the correct placement of the release the L key command to have the walls solid again, but I think that should work and if it doesn't, please let me know what errors you get.

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 anjobee · Aug 28, 2019 at 11:35 AM 0
Share

Okay, this worked. But the problem is, whenever I set collision.enabled to false it never goes back to true.

public class PhaseCheck : $$anonymous$$onoBehaviour { public GameObject walls; private CompositeCollider2D wallcollider;

 PlayerInput input;                      //The current inputs for the player
 BoxCollider2D bodyCollider;             //The collider component
 Rigidbody2D rigidBody;                  //The rigidbody component



 // Start is called before the first frame update
 void Start()
 {
     Debug.Log("Game");
     wallcollider = walls.GetComponent<CompositeCollider2D>();
     if(wallcollider == null)
     {
         Debug.Log("No Collider Detected");
     }
     else
     {
         Debug.Log("Collider Detected");
     }
 }

 // Update is called once per frame
 void Update()
 {
     Phasing();
 }

 void Phasing()
 {
         if (Input.GetButton("Phase"))
         {
             wallcollider.enabled = false;
         }
         else if (Input.GetButtonUp("Phase"))
         {        
             wallcollider.enabled = true;
         }
     
 }

}

avatar image I_Am_Err00r anjobee · Aug 28, 2019 at 07:07 PM 0
Share

So, like I said I'm not a wiz at coding, saw I was trying to convert my List of GameObjects to a Collider2D and that is why you had the error with your foreach loop (as well as populate a List in Start in a way it can't be populated by a List, I just learned that now, but you can definitely populate an array like that), so my apologies for giving you a buggy answer.


HOWEVER, I did try your solution, and your solution will work UNDER THE CONDITION you don't use a CompositeCollider2D; maybe this is a bug with Unity, I don't know but I could not get a CompositeCollider2D to turn back on regardless of how I called it or referenced it, and could only disable it not enable; if there is a way to do this, I didn't find it out in the 15 $$anonymous$$utes I toyed with this to give you a correct answer.


I am making a 2D sidescrolling game and tried using a Tile$$anonymous$$ap as well to control things like doors and elevators (script controlled objects mainly) and had a hard time with the elevators and doors not moving where I wanted an required a ton of guesswork in getting the EXACT position I needed in world coordinates for these situations, and would recommned you make the walls that you want to walk through have a BoxCollider or something else, you will probably also want to remove these wall types from your tile map and manually add them into your scene as prefabs (I know, I felt the same way when I realized I was simply wasting too much time trying to get this Tile$$anonymous$$ap to work when if I just used prefabs it would be a thousand times easier to manage and control).


If you wanted to use the first method I provided, change list to an array like this:

 GameObject[] walls;

And as long as you set a tag on the walls like I put in that example above, you can populate that array right at start and then in the foreach loop, you just need to change to this:

 foreach(GameObject wall in walls)
 {
     Collider2D col = wall.GetComponent<Collider2D>();
     if(Input.Get$$anonymous$$eyUp("Phase")
        {
            col.enabled = true;
        }
        else if(Input.Get$$anonymous$$ey("Phase")
        {
            col.enabled = false;
        }
 }

I tested that about an hour ago and it worked with a BoxCollider2D in the example I used, but I think any collider2D besides Composite it will work


EDIT I just thought about this, but maybe try setting the CompositeCollider2D to a trigger as true and set it to trigger as false and that might actually work for you (depending on how you detect collisions). So do do this for your phasing method:

 void Phasing()
  {
          if (Input.GetButton("Phase"))
          {
              wallcollider.isTrigger= true;
          }
          else if (Input.GetButtonUp("Phase"))
          {        
              wallcollider.isTrigger = false;
          }
  }
avatar image anjobee I_Am_Err00r · Aug 31, 2019 at 04:05 PM 0
Share

Thanks, bro. Yes, everything you said was true. I can't tick enable again with the composite collider. I guess I'll be using prefabs if need be because of another problem that came up which makes my enemies also pass through walls which I don't want it to do. Is there another method where I'm like going into limbo? Like travel in another dimension but with same settings, map, platforms but able to pass through some walls or able to see pathways that aren't seen in the real world? Sorry if this went out of the main question.

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

246 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 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

Tilemap Collision 2D colliding with ground. 0 Answers

2D jump problem when hits wall 0 Answers

Player getting stuck in colliders that are not touching each others 1 Answer

[C#][Physics2D] multiple colliders and OnCollisions on one gameobject 0 Answers

Circle Collider2D Stuck ob Box Collider2D at the corner :( 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