Hey, I’m currently adding some obstacles to my game, some of which I would like to be able to jump up through, and then land on. And when I want to fall back down, press down-arrow, and fall back down through it.
Here is what I’ve done so far:
I’ve added an empty gameobject called groundcheck, put it at my player’s feet, and parented it to him. Then I’ve put my obstacles in a layer called obstacles. Then I’ve written this script and put it on my obstacles.
using UnityEngine;
using System.Collections;
public class Collider_on_off : MonoBehaviour {
bool grounded = false;
public Transform obstaclecheck;
float groundradius = 0.5f;
public LayerMask whatisground;
// Use this for initialization
void Start () {
collider2D.enabled = false;
}
// Update is called once per frame
void Update () {
if (grounded)
Debug.Log ("Grounded is true");
}
void FixedUpdate() {
grounded = Physics2D.OverlapCircle(obstaclecheck.position, groundradius, whatisground);
if (grounded)
collider2D.enabled = true;
}
}
In theory it should work like this: my collider is set to false, and when grounded is true, so is my collider. It becomes true by the grounded variable in FixedUpdate. My gameobject at my feet should check for the layer “obstacle” (which is set in the inspector), and when it hits it, grounded should be true.
Here is my problem: Since my collider2D.enabled = false, my gameobject doesn’t register the layer on it, and “grounded” never becomes true. If I have collider as enabled, it becomes true (so my script is sort of working).
How do I get it to register my obstacle, even its collider is off?
Thanks