- Home /
2d platformer, ignorelayercollision
Hello all,
I am attempting to implement a 2d platformer where the player can pass through the platform then it solidifies allowing the player to stand on it. The code I currently have seems to behave irregularly, sometimes acting like it should and most times pushing the player back toward the ground.
Currently my platform object looks like this:
With this hierarchy:
The script attached to the below object is:
using UnityEngine;
using System.Collections;
public class parentController : MonoBehaviour {
bool inObj;
void Start () {
inObj = false;
Physics2D.IgnoreLayerCollision(8,10,false);
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D player){
if (player.name == "player"){
inObj = true;
Physics2D.IgnoreLayerCollision(8,10,true);
}
}
void OnTriggerExit2D(Collider2D player){
if (player.name == "player"){
inObj = false;
}
}
public bool getBool(){
Debug.Log (inObj);
return inObj;
}
}
The script attached to the above object is as follows:
using UnityEngine;
using System.Collections;
public class JumpThrough : MonoBehaviour {
//public GameObject player;
GameObject player;
public parentController test;
void OnTriggerStay2D(Collider2D player){ //also tried with enter
if (player.name == "player" && !test.getBool()){
Physics2D.IgnoreLayerCollision(8,10,false);
}
}
// Use this for initialization
void Start () {
player = GameObject.Find ("player");
test = transform.parent.GetComponent<parentController> ();
}
void Update(){
if (Input.GetKey (KeyCode.S)) {
Physics2D.IgnoreLayerCollision(8,10,true);
}
}
}
Can anyone point out what I may be doing wrong?
what if you deactivate collider when the player is moving up and enable it when player is falling -y-axis
I tried that but that behaves irregularly too (player is jumping from side) which is why I was trying this way
Your answer
Follow this Question
Related Questions
How to get camera to follow player 2d 11 Answers
2D Character Movement Collision Problem 0 Answers
player can stick to wall 2 Answers