I have a really weird one here, and also pretty long, so bear with me. I’m working on a little platformer, and i want a sound effect to be played when the player collides with the ground. As I am working with a Character Controller for the player movement, i added another Box Collider to the Player object to check for particular collisions.
The player object currently has a character controller, as well as a box collider (with isTrigger == true) attached to it, as well as a script controlling the character controller (which i didn’t write myself). So far movement works pretty well, but now for the sound effect to be played.
First i tried it with a OnCollisionEnter function on the player object, which i simply filled with a Debug.Log statement, to find out if it even recognizes the collision, but this didn’t do anything at all. No collision with the collider.
Then i tried it with OnTriggerEnter on the ground object. This seemed to work at first, the log displayed my debug message and the sound effect was played. for this i instantiate a empty GameObject with the AudioSource added (which i added as a prefab) and so far, it worked well.
Alright i think, happily, this works as intended. So i shoot the two enemy object are also on the screen. They explode. And then, silence. No soundeffect plays anymore. The landing-collision isn’t detected anymore. The whole OnTriggerEnter function stops working.
This really bugs me and i can’t for the life of me tell why this happens. The enemy objects don’t have anything to do with the ground object, other than they both use a OnTriggerEnter function, but of course in two different scripts.
i also tried deactivating the enemies completely before starting the game, but then the same problem appears. From the start, i don’t hear any landing sound effect at all.
Here’s the relevant code:
Enemy.cs (only partial)
void OnCollisionEnter(Collision other)
{
if (other.collider.tag == "projectile")
{
if (HP > 1)
HP--;
else if (HP <= 1)
{
Instantiate(destroySoundObject);
enemyKilled();
Destroy(gameObject);
}
}
}
// when the player touches the enemy
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("Player touched");
CharacterController cc = other.GetComponent<CharacterController>();
// knockback the player
if (other.transform.position.x < transform.position.x) // when player is left of enemy
cc.Move(new Vector3(-Knockback, 1, 0));
else // when player is right of enemy
cc.Move(new Vector3(Knockback, 1, 0));
}
}
// emit particles when enemy is killed
void enemyKilled()
{
for (int i = 0; i <= NumberOfParticles; i++)
{
Rigidbody newParticle = Instantiate(ParticleObj, transform.position, Quaternion.identity) as Rigidbody;
newParticle.AddForce(new Vector3(Random.Range(-10f, 10f), 18, Random.Range(-1000f, 1000f)), ForceMode.VelocityChange);
}
}
GroundSFX.cs
void OnTriggerEnter(Collider othercollider)
{
if (othercollider.tag == "Player")
{
Debug.Log("Player on ground");
Instantiate(landingSE);
}
}
the goundSFX.cs script is attached to the ground. However, when I add it to other environment, such as boxes (which have pretty much the same properties as the ground object) it doesn’t work either.