Search code examples
unity-game-engineunityscript

Compare tags in UnityScript


We are building a 3D game and right now I am stuck on an issue where I need to compare multiple tags to activate or deactivate a trigger that sends the player back to the respawn position.

This is currently the code:

#pragma strict

var spawnPoint : GameObject;

function OnTriggerStay ( other : Collider )
{
    if (other.tag == "Player" && other.tag != "Ball")
    {
        other.tag == "Player";
        other.gameObject.transform.position = spawnPoint.transform.position;
    }

    else if ( other.tag == "Ball" && other.tag == "Player" )
    {

    }
}

I am uncertain how to fix this to do the following:

If the player touches the trigger without there being a ball colliding with it, the player respawns. This is to create the feeling that the particles kill you. If the player touches the trigger when there is a ball colliding with it as well, nothing happens to the player and so the player can freely pass through.

What we want to do is that we want to push a ball over a geyser so it covers it and if the geyser is not covered and the player tries to pass over it, the player respawns.

We also tried with another code and while it allows the ball to pass, it does not allow the player to do so. Even after the ball has been placed.

#pragma strict

var spawnPoint : GameObject;

function OnTriggerStay ( other : Collider )
{
    if (other.tag == "Ball")
    {
        other.enabled = false;
    }

    else if (other.tag == "Player")
    {
        other.gameObject.transform.position = spawnPoint.transform.position;
    }
}

Solution

  • So, I believe your problem is, when you use that function, you are only checking the first collision. So whats happening is your getting a value of either the player or the ball, not both. You need to store all the collision so you can compare all of them. To do that you can follow the generals of this documentation. http://docs.unity3d.com/ScriptReference/Collision-contacts.html It's talking about collisions, but the same general principle should apply.

    Hope this helps!