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;
}
}
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.
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;
}
}
}
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;
}
}
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
Follow this Question
Related Questions
[C#][Physics2D] multiple colliders and OnCollisions on one gameobject 0 Answers
Circle Collider2D Stuck ob Box Collider2D at the corner :( 0 Answers
Player is bouncing when colliding with wall object 0 Answers
2D Collider tolerance? Character Getting Rooftopped. 0 Answers
Collider gets sometimes stuck at wall 0 Answers