- Home /
get the OnCollisionEnter or Stay from the parent?
Hi there!
I have here a little problem going on. I have a Cube with a BoxCollider and a Rigidbody on it.
I want to have sound effects from the Cube when it hits something with a specific force and when the Cube is sliding on the floor. So I wrote a script with OnCollisionEnter and OnCollisionStay and a AudioSource and put them into the Cube.
but now if the Cube hits the floor the OnCollisionEnter sound is canceled right after that because it will sometime slide on the floor a little bit and so the OnCollisionStay overwrites the sound that should to be played with the sliding sound.
so I came up with the Idea to make 2 AudioSources but on One Gameobject it wont work. so I put 2 emptys with AudioSources on the Cube and rewrote the script for each.
but now I dont get any respond from the scripts and thats because the scripts dont have any Rigidbodys or colliders...
so how do I get the collision info to the child emptys?
here is the impact script from the child object.
#pragma strict
@script RequireComponent(AudioSource)
var impactMagnitude : float = 0.01;
var crVelocity : Vector3;
var crMagnitude : float;
function OnCollisionEnter (collision : Collision)
{
// Debug-draw all contact points and normals
for (var contact : ContactPoint in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
crVelocity = collision.relativeVelocity;
crMagnitude = collision.relativeVelocity.magnitude;
var propInfo = transform.parent.gameObject.GetComponent(Prop_Info);
if (collision.relativeVelocity.magnitude > impactMagnitude)
{
audio.clip = propInfo.PropSoundFX.impact[Random.Range(0,propInfo.PropSoundFX.impact.length)];
audio.volume = (collision.relativeVelocity.magnitude / 5 + 0.5);
audio.pitch = (Random.value * 0.4 + 0.8);
audio.Play();
}
}
don't know nothing about audio but did you try making 2 audio outputs?
like let say if you make only 1 int variable you overwrite it when you add it a new value.
so making 2 int-s makes you ability to have 2 values, ...
so having 2 audio outputs somehow.
OR if that's not possible as I looked in to your code what about making a child empty game object with audio component and using that audio for floor, ...
but that drives me insane for character to be able to have 30 sounds at once possible that would have to have 30 chldren for it, ...
yes that is that what bothers me. If I would need to play almost 30 sounds from 1 object at the same time. (theoretically) then I would need 30 children with audiosources.... it would be easier if I could give the audiosources names in the editor like audiosource 001 audiosource 002 and so on
then I would be able to have more audiosources from 1 Gameobject and I could access it via script and all that in one Gameobject.
but thank good no one on earth would do such a horrible thing... 30 sounds at the same time lol.... ^^
please read my answer down there.
Answer by danilonishimura · Jul 31, 2013 at 09:18 PM
The sound is canceled because you're using the same audio source for both sounds. Perhaps you should ignore the audio property of the GameObject and use two AudioSource objects and play the hit sound in one and the slide sound in other.
Answer by Harry64 · Jul 31, 2013 at 11:10 PM
sooo.... what you 2 say is I should not use the OnCollisionEnter or Stay function in the empty Child objects because the children dont have any collider and its not possible to send the Collision info from the parent to the children??
and instead I should write the script for the Cube and I should just make 2 var : Audiosource in the script and tell the script on awake it should look in the children for the audiosources? I think that sounds ok.
but really, is it not possible to send the
function OnCollisionEnter(collision : Collision)
information from the collsion that happens to the children?