- Home /
My instanced projectiles will not trigger the OnTriggerEnter command on enemies
Hi!
New to this, getting a little frustrated and wondering why it won't work...
Have these projectiles that are being instanced when I click the screen, the code on the game is as follows:
public Rigidbody banana;
public float shotForce = 1000f;
public Transform shotPos;
void Update () {
if(Input.GetMouseButtonDown(0))
{
Rigidbody shot = Instantiate(banana, shotPos.position, shotPos.rotation) as Rigidbody;
shot.AddForce(shotPos.forward * shotForce);
}
}
}
The code that is attached to the "enemies" that should destroy said enemies when the projectiles enter their collider is:
private GameObject banana;
void Awake ()
{
banana = GameObject.FindGameObjectWithTag(Tags.banana);
}
void OnTriggerEnter(Collider other) {
if(other.gameObject == banana)
{
//Instantiate (explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}
and all of this is working with a tag manager which is in the scripts folder which is simply :
public const string banana = "Banana";
public const string player = "Player";
The projectile prefabs collider has Is Trigger unchecked. The projectile prefabs rigidbody has Use Gravity Checked and Is Kinematic unchecked.
The enemy prefabs collider has Is Trigger checked. The enemy prefabs rigidbody has Use Gravity unhecked and Is Kinematic checked
The strange thing is that when I put a projectile prefab in the scene and let it fall onto the enemy it works, but when I am shooting the instanced ones at them it doesn't.
I then tried putting an empty game object that instanced without having to shoot, it just keeps instancing projectiles with:
public Rigidbody banana;
public Transform shotPos;
void Update () {
Rigidbody shot = Instantiate(banana, shotPos.position, shotPos.rotation) as Rigidbody;
}
}
and it instances one on top of the other...but they do not fall with gravity, like the original that I put into the scene by hand, they just float there!
what is it that is wrong? please help!
Answer by neurograph · Sep 02, 2013 at 10:49 AM
Is your problem about physics or about triggering? I don't know, but for the target triggering I think you don't need the Awake function in your enemy script... Have you tried simply adding .tag to the other collider in the OnTriggerEnter function??
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "banana")
{
//Instantiate (explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
Answer by Eudaimonium · Apr 10, 2016 at 10:44 AM
I do not understand the first part of your code - you are instantiating a Rigidbody? Why? Rigidbody has no colliders. When you drop a prefab into the scene, it's probably a complete GameObject with colliders, rigidbody and script.
Instead of public RigidBody banana;
, use:
public GameObject bananaPrefab;
Assign the prefab via the Inspector.
Then, instantiate the gameobject:
GameObject shot = Instantiate(bananaPrefab, shotPos.position, shotPos.rotation) as GameObject;
shot.rigidBody.AddForce(shotPos.forward * shotForce);
Your answer
![](https://koobas.hobune.stream/wayback/20220613112919im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make multiple Projectiles move at the same Time... 0 Answers
Fire Projectile based on Model rotation 1 Answer
Velocity and AddForce Problems 0 Answers
Applying force to a rigidbody 2 Answers
Prefab tag bug 0 Answers