0
\$\begingroup\$

I'm having some trouble working out what's wrong with my code. Basically I want to have my player be able to collide with obstacles it's flying through.

So I have an obstacle, and a pathway or two for the player to go through, if they don't make it into the pathway and hit the obstacle, they'll die, but if they do make it in, I need the floor and roof of the path to stop my player from going through it.

Here is what I've got so far (sorry for the copious amounts of code, I've tried to compress it down to the bare essentials for you but it's still a lot):

void OnTriggerEnter2D(Collider2D obstacles) {

    if (obstacles.gameObject.tag == "Obstacles") {
        playerInObstacle = true;
    }

    if (obstacles.gameObject.tag == "Path") {
        playerInPath = true;
        path = obstacles;
    }
}

void OnTriggerExit2D(Collider2D obstacles) {

    if (obstacles.gameObject.tag == "Obstacles") {
        playerInObstacle = false;
    }
    if (obstacles.gameObject.tag == "Path") {
        playerInPath = false;
    }
}

void Update() {

    playerY = transform.position.y;

    if (playerY > Y_Max) {
        aboveClouds = true;
    } else {
        aboveClouds = false;
        hasJumped = false;
    }

    if (aboveClouds == false) {

        if (Input.GetKey (KeyCode.UpArrow)) {

            ySpeed += horizontalSpeed * Time.deltaTime;
        }
    }

    //a bunch of other movement stuff is actually in this section, if you need that, let me know and I'll post it.

    if (timer >= jumpTime) {
        hasJumped = true;
        timer = 0;
    }

    if (aboveClouds == false) {
        hasJumped = false;
    }

    if (ySpeed > ySpeedLimit) {
        ySpeed = ySpeedLimit;
    } else if (ySpeed < -ySpeedLimit) {
        ySpeed = -ySpeedLimit;
    }

    transform.position += Vector3.up * ySpeed * Time.deltaTime;

    //floor collisions (this one works)

    if (playerY < Y_Min) {
        floorOn = true;
        ySpeed = noSpeed;
    } else {
        floorOn = false;
    }

    //path & obstacles collisions (this one doesn't work)

    if (playerInObstacle == true) {

        if (playerInPath == true) {

            if (playerY > path.bounds.max.y) {
                ySpeed = noSpeed;
            } else if (playerY < path.bounds.min.y) {
                ySpeed = noSpeed;
            }
        }
    }

    playerY = transform.position.y;

}

Also, I originally tried having the onTriggerEnter2D's as separate entities for both the obstacles tag and the path tag, but apparently you can't do that, so maybe I'm doing this part wrong too..

All help is greatly appreciated, thank you!

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm having trouble figuring out exactly what you are trying to do and why you are only using triggers. Wouldn't it be easier to use colliders for the floor and roof, as well as a "kill zone" trigger? \$\endgroup\$ – Bluk Jul 28 '15 at 15:42
  • \$\begingroup\$ Perhaps, I'm not too sure, but I did try colliders and because these objects are moving past my player to simulate movement (it's a 2D sidescroller), when they hit the roof or floor, they did a jerkimg motion in that it wasn't a smooth sail across the floor or roof of the pathway. \$\endgroup\$ – S_Byrnes Jul 28 '15 at 21:16
  • \$\begingroup\$ Ok so you have moving objects , a player, and the roof/Floor. You want the player to be killed by the moving objects, collide with the roof/floor, right? On top of that, you want the moving objects not to collide with the roof/floor? \$\endgroup\$ – Bluk Jul 28 '15 at 22:18
  • \$\begingroup\$ Uhh... well about that last part, the moving objects contain the roof/floor, for example I have these pillars, and they have paths that the player can go through, the pathways have a "roof" and a "floor" that you can collide with while you're inside of the pillar. \$\endgroup\$ – S_Byrnes Jul 28 '15 at 23:12
  • \$\begingroup\$ Do you happen to know what's wrong with the code Bulk? - Most it's the area below the "//this one doesn't work" part. \$\endgroup\$ – S_Byrnes Jul 30 '15 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.