- Home /
OnTriggerEnter() not firing for mutliple objects
So I have a Shield PowerUp that when the player runs into gets attached making the player invincible until the player hits an obstacle. When the player hits an obstacle the shield should be destroyed thus setting the invincibility back to false.
I am trying to use either OnCollisionEnter or OnTriggerEnter but neither are giving me the behavior I desire.
For OnCollisionEnter since I am having the shield objects position the same as the player, it will launch the player to the side of the screen.
For OnTriggerEnter it will not fire after attaching to the player.
Desired Behavior
When player collides with shield equip it to player
Set player to invincible
In item update assign position to players
If collides with obstacle set invincibility to false and destroy shield
Here's my code with both OnCollisionEnter and OnTrigger Enter (I believe only one should be used but don't know how to go about it)
using UnityEngine;
using System.Collections;
using SmoothMoves;
public class Shield : PowerUp
{
public int shieldRadius;
public Collider shieldItemCollider, shieldPetCollider;
public ShieldTrigger shieldTrigger;
public MeshRenderer meshRenderer;
// Use this for initialization
void Start () {
Debug.Log ("HELLO I'M A SHIELD");
}
// Update is called once per frame
void Update () {
//Debug.Log("Whats going on");
}
public override void ItemUpdate ()
{
transform.position = tempPet.transform.position;
base.ItemUpdate ();
}
//pretty much same thing as OnTriggerEnter
void OnCollisionEnter(Collision col)
{
if(col.gameObject.GetComponent("Pet") != null)
{
tempPet = (Pet)col.transform.parent.gameObject.GetComponent("Pet");
tempPet.powerUps.Add(this);
tempPet.isInvincible = true;
ObstacleManager.obstaclesInSceneList.Remove(this);
}
if(col.gameObject.GetComponent("Obstacle") != null)
{
DestroyShield ();
}
}
//Pretty much same as OnCollisionEnter()
void OnTriggerEnter(Collider col)
{
if(col.transform.parent.gameObject.GetComponent("Pet") != null)
{
tempPet = (Pet)col.transform.parent.gameObject.GetComponent("Pet");
tempPet.powerUps.Add(this);
tempPet.isInvincible = true;
ObstacleManager.obstaclesInSceneList.Remove(this);
}
if(col.transform.parent.gameObject.GetComponent("Obstacle") !=null)
{
Debug.Log("OBSTACLE");
DestroyShield();
}
}
public void DestroyShield()
{
tempPet.isInvincible = false;
isDestroy = true;
}
}
Your answer
Follow this Question
Related Questions
Little problem with OnTriggerEnter 1 Answer
Can not get OnTriggerEnter and OnCollisionEnter to work!? 1 Answer
Colliders that do not collide... 1 Answer
function OnCollisionEnter does not work 4 Answers
OnTriggerEnter/OnCollisionEnter and 5.0 3 Answers