- Home /
Question by
kitommy · Sep 03, 2015 at 09:28 AM ·
2d-platformer2d-gameplayboxcollider2d
Problem with platformer game
I have a group of objects ( each object has Boxcollider2D ) with 1 leaders I have the Platform ( Platform offers Boxcollider2D each ) located in two different layer I want my group when running on a platform of layer 1 can jump onto the platform of layer 2 I checked the location of the platform under layer 2 ( if it is smaller than the position of my leader , the Box Collider is enabled , even if smaller , the disabled )
But the problem is that only leaders can jump onto the platform of layer 2 remaining members of the group were stranded because of the platform BoxCollider layer 2 has been enabled
How can i Fix that??
//My Platform of layer 2 script
public class PlatformLayer2 : MonoBehaviour
{
public GameObject target;
BoxCollider2D box;
// Use this for initialization
void Awake ()
{
target = GameObject.Find ("ground0");
box = this.gameObject.GetComponent<BoxCollider2D> ();
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
target = GameObject.Find ("ground0");
if (target != null) {
if (transform.position.y < target.transform.position.y) {
box.enabled = true;
}
if (transform.position.y >= target.transform.position.y)
box.enabled = false;
}
}
}
//my ground with 1 leader script
public class Mygroup : MonoBehaviour
{
public float time;
public float jump;
public bool grounded;
public GameObject target;
public string targetName;
public Transform ground;
public bool isDie, jumping;
public float speed;
Animator anim;
// Use this for initialization
void Awake ()
{
isDie = false;
}
void Start ()
{
Debug.Log ("Da tim thay target" + targetName);
target = GameObject.Find (targetName);
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
if (this.gameObject.name == "P0") {
targetName = "";
Transform g = this.gameObject.transform.FindChild ("ground");
if (g != null)
g.gameObject.name = "ground0";
} else if (this.gameObject.name == "P1")
targetName = "P0";
else if (this.gameObject.name == "P2")
targetName = "P1";
else if (this.gameObject.name == "P3")
targetName = "P2";
else if (this.gameObject.name == "P4")
targetName = "P3";
else if (this.gameObject.name == "P5")
targetName = "P4";
else if (this.gameObject.name == "P6")
targetName = "P5";
else if (this.gameObject.name == "P7")
targetName = "P6";
else if (this.gameObject.name == "P8")
targetName = "P7";
else if (this.gameObject.name == "P9")
targetName = "P8";
else if (this.gameObject.name == "P10")
targetName = "P9";
else if (this.gameObject.name == "P11")
targetName = "P10";
target = GameObject.Find (targetName);
}
// Update is called once per frame
void Update ()
{
Physics2D.IgnoreLayerCollision (9, 9);
float dir = new float ();
if (target != null)
dir = target.transform.position.x - gameObject.transform.position.x;
if (dir > 0.5f && grounded) {
gameObject.transform.Translate (Vector2.right * speed);
}
grounded = Physics2D.Linecast (transform.position, ground.position, 1 << LayerMask.NameToLayer ("ground"));
if (grounded) {
jumping = false;
}
if (Input.GetMouseButtonDown (0) && gameObject.name == "P0" && grounded) {
jumping = true;
rigidbody2D.velocity = new Vector3 (0, 50 * jump, 0);
}
if (target != null && grounded && target.GetComponent<Mygroup> ().jumping && dir <= 2f) {
StartCoroutine (Jump ());
}
anim.SetBool ("run", grounded);
}
IEnumerator Jump ()
{
yield return new WaitForSeconds (time);
jumping = true;
rigidbody2D.velocity = new Vector3 (0, 50 * jump, 0);
}
}
Comment