Have a few question to ask about trigger and tag. From the title above I know that you can check what gameObject are triggering the trigger by the tag that the game object have, example :
static var onHit : boolean;
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
onHit = true;
}
}
Okay, now for the question. Can I check if the player are triggering the right gameObject by tag. example:
this is some pseudo code that I think how it should be done, well not sure about it anyway.
static var onHit : boolean;
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player" objObject.tag == "P01")
{
onHit = true;
}
}
I did a nested if-else loop to check for two different object tags (the one that collided and the one that was collided). It works so I’m guessing that if you do ‘and’ in the if-loop, it should work.
Thanks @wolfhunter777 and @Mig-081 for the quick answer, It does work.
But need to check something because right now when the player hit the gameObject it does detect the gameObject tag but it destroy all of the gameObject at once.example:
#pragma strict
public var objConfetti : GameObject;
public var objObject : GameObject;
public var objRotate : int;
static var onHit : boolean;
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
if (gameObject.tag == "P01")
{
onHit = true;
print ("this is: " + objObject.tag);
}
else if (gameObject.tag == "P02")
{
onHit = true;
print ("this is: " + objObject.tag);
}
else if(gameObject.tag == "P03")
{
onHit = true;
print ("this is: " + objObject.tag);
}
else
{
onHit = false;
}
}
}
function Update ()
{
kill ();
}
function kill ()
{
if (onHit == true)
{
Instantiate (objConfetti, transform.position, transform.rotation);
Destroy (objObject);
}
else
{
transform.Rotate (Vector3.up * objRotate * Time.deltaTime);
}
}
@Mig-081, the Destroy part is fine, I like to Destroy stuff haha:)
The problem that i got is it destroy all the gameObject that are not spesific tag.Example, If i trigger tag “P01” it will also destroy all the other gameObject which is tag “P02” and P03".
From what I’m seeing, if the player collides with objObject (which the script is attached to), you want to destroy that game object yes? From what I did in C#, I used Destroy(this.gameObject).
How about you change the script so that the script is attached to the player. When the player collides with the collided object, you destroy the collided object.
Another way to go about it is to do it from the obstacle. Since the script is going to be attached to all the objects to be destroyed, you just have to destroy that object.
There’s some messed up stuff going on there, OnTriggerEnter only fires on the object that was collided with. Your issue is you are using the class variable ManagerPowerUp.onHit. A class variable is the same for every instance of the class.
There’s no need to check the tag of the object unless you want to use one piece of code to handle several different classes of objects (and tags is still a bad solution to this).
You could make the onHit variable a private variable and refer to it in your code as onHit (without the ManagedPowerUp bit in front). However there’s likely no need to call the kill function in update. Just call it from the collision.
So remove your update method, and change OnTriggerEnter to:
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
kill();
}
}
Have you tried our suggestions? Also, if the script is going to be attached to the objects, why must you pass the object that the script is attached to into the script? Can’t you use this.gameObject? But then again, I have only coded in C# so I don’t know how Javascript works.
Yup tried it and it does work, I did used the way you suggested before I change my script to this. just wanted to learn different type of scripting structure. I forgot to change the “objObject” to “gameObject” sorry about that.
yeah, just double check, just like wolfhunter777 said
//check is it picked up by player
if (hit.gameObject.tag == “Player”)
{
if (target.gameObject.tag == “Player”)
{
Destroy(gameObject);
//add health to the player
PlayerGUI eh = (PlayerGUI)target.GetComponent(“PlayerGUI”);
eh.adjustHealth(100);
//add mana to the player
eh.adjustMana(100);
}
}
yeah, just double check, just like wolfhunter777 said
//check is it picked up by player
if (hit.gameObject.tag == “Player”)
{
if (target.gameObject.tag == “Player”)
{
Destroy(gameObject);
//add health to the player
PlayerGUI eh = (PlayerGUI)target.GetComponent(“PlayerGUI”);
eh.adjustHealth(100);
//add mana to the player
eh.adjustMana(100);
}
}
I guess not a big issue on desktop but tags are quite slow. They also use string based comparison so can lead to coding error.
Without any detailed knowledge of exactly what you are doing I’d suggest an enum and an instance variable on the script.
EDIT: The alternative to using tags is for example to use layer based collision… if you only care about a player touching an object then you can set the physics engine to only trigger collisions between players and the object.
I think you are still missing the point about class variables, thats a terrible way to manage things, every time you add a new object you will need to add a new variable.
It much easier (and more efficient) to call a function on the manager object than to set a flag and check for it in update.
Try removing update code for your manager and doing something like:
public var objConfetti : Transform;
public var objObject : GameObject;
public var objRotate : int;
[b]public var powerUpManager: ManagerPowerUp;[/b]
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
if (objObject.tag == "P01")
{
kill ();
[b]powerUpManager.MainPick();[/b]
}
else if (objObject.tag == "P02")
{
kill ();
powerUpManager.Ice();
}
else if(objObject.tag == "P03")
{
kill ();
powerUpManager.StopTime();
}
}
}
EDIT: Dont forget to link your power ups to the power manager in the editor.