Compare tags in UnityScript - unity3d

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;
}
}

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!

Related

Force occasionally being applied twice with OnCollisionEnter

I am creating a first person rocket jumping game, with the premise of shooting a rocket launcher at the players feet to move around.
I am having problems with my OnCollisionEnter function which stores all colliders in a radius and applies explosion force to them. When the player is completely on top of the explosion, force is being applied twice. Here is the code:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
}
else
{
GetComponent<AudioSource>().Stop();
Instantiate(explosionPrefab, transform.position, transform.rotation);
Vector3 explosionPos = transform.position;
//Use overlapshere to check for nearby colliders
Collider[] collidersToDestroy = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in collidersToDestroy)
{
//searches for what needs to be destroyed before applying force, this is for destructable objects
Destructible dest = hit.GetComponent<Destructible>();
if (dest != null)
{
dest.DestroyWall();
}
ExplosiveBarrel barrel = hit.GetComponent<ExplosiveBarrel>();
if (barrel != null)
{
barrel.BarrelExplode();
}
}
Collider[] collidersToMove = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in collidersToMove)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
//Add force to nearby rigidbodies
if (rb != null)
{
rb.AddExplosionForce(power * 5, explosionPos, radius, 3.0F);
if (hit.gameObject.tag == "Player")
{
//if player is hit
UnityEngine.Debug.Log("Hit");
}
}
Destroy(gameObject);
}
}
}
I can tell the force is being applied twice to the player by using UnityEngine.Debug.Log("Hit"); , which appears twice in the console. Furthermore, I am pretty sure this is happening on the same frame, as putting Destroy(gameObject); within the if (player hit) statement yields the same results.
This only occurs when the player is right next to the explosion, if the player is a small distance away the force is only applied once. I would very much like to solve this problem and have the force only applied once.
All help is greatly appreciated, thank you in advance.
I solved it!
OnCollisionEnter was being called twice because I had two colliders perfectly stacked on one another, so the projectile was hitting the 2 colliders on the same time step.

Can't detect collision with child and ground

I have the below script attached to the ground game object to detect collisions from my players' child objects but for some reason, the collisions are not detecting.
My player (parent, empty game object) has a Rigidbody and Jump script attached to it, meanwhile, the child game objects (body & feet) have just box colliders on them.
Would love to know why this isn't working :)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Body")
{
Debug.Log("Game over!");
}
else if (collision.gameObject.tag == "Feet")
{
Debug.Log("Alive!");
}
}
For some reason, it works when you do collision.collider.tag instead of collision.gameobject.tag, like so:
if(collision.collider.tag == "Head")
{
Debug.Log("Game over!");
}
Not exactly sure why this works, but it works! Defo going to look into this a bit more.

Falling Platform using rigidbody

I have a platform that i want to fall once the player has stepped on it. It has two box colliders. One for it's physical being and the other as a trigger. It also has a rigidbody which is kinematic by default so it doesn't fall straight away. However when the player steps on it it does nothing. Can any one tell me what's wrong with my code?
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
Plase check your gameobjects tag.
and also i see you use Rigidbody2D so you don't add below
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
You need to use this
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
And also please check you add tag to gameobjects (I think you mean check names)
Like
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.name == "Player"){ //check name
yourObject.rigidbody2D.isKinematic = false;
}
}
To achieve a falling platform of sorts you do not have to make use of the IsKinematic function.
Instead, you can just turn off the Use Gravity function. This will prevent the object from falling, until another object with a rigidbody and a mass bigger then the mass of the platform touches it. As can be seen in the GIF below.

how to destroy an item after collision

I am creating an fps game, I have created the gun, the bullet, and the enemy.
Now I want to make my enemy destroyed after the collision with bullet.
My enemy is a gameobject named Fire and tagged Enemy and my bullet named "Cube 1(Clone)" and tagged "Cube 1(Clone)". I made a script for that:
#pragma strict
function OnTriggerEnter(theCollision : Collider)
{
if(theCollision.gameObject.name=="Cube 1")
{
Destroy(gameObject);
Debug.Log("Dead");
}
}
But it does not work.
You need to check the tag not the name. You could check for name but remember it will have "(Clone)".
function OnTriggerEnter(theCollision : Collider)
{
if(theCollision.tag == "Cube 1")
{
Destroy(gameObject);
Debug.Log("Dead");
}
}
If you are not sure that you tagged correctly, you can simply use both checks in your if statement.
if(theCollision.tag == "Cube 1" || theCollision.gameObject.name == "Cube 1(Clone)")
Destroy(gameObject);
Well since the bullet is tagged Cube 1(Clone) , I would use
if(theCollision.tag == "Cube 1(Clone)"){...}
And would probably would rename the tag to something meaningful, say bullet.

onCollsionEnter specifying objects?

I'm currently making a infinite runner type game for a class project and I need some help with collisions.
The game is set up as a 2d platform where a character is running (or made to look like they're running with a scrolling texture) through an infinite tunnel. The player has to avoid obstacles similar to Flappy Bird using jumps. The script I'm using at the moment is setup so that when the player collides with obstacles the game will reload.
The issue:
The issue is that the code I've used applies to all collisions so when the player hits the ground platform the game reloads.
What I want to happen:
I want the character to be able to run on a platform and die when he hits the a specific obstacle.
So I'm think I need to specify a GameObject to collide with?
This is my current code:
// Update is called once per frame
void Update ()
{
// Jump
if (Input.GetKeyUp("space"))
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}
// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Use tags. For example, use a terrain tag for the ground. If the object tag is terrain, then don't die.
Here is a simple example:
void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.tag == "Terrain")
{
Debug.Log("Don't die!");
}
else
{
Debug.Log("Die!");
}
}