Hey there, I’m rather green with Unity and javascript. Got a few questions:
First of all… I know a gameObject can only have one tag, however can multiple gameObjects in a scene share the same tag?
I’m trying to create an OnCollisionEnter function that recognizes precisely what gameObject it has collided with and play an AudioClip accordingly. I know collisionInfo includes the gameObject information, I just can’t figure out how to discern between different objects within the function:
var trigger_magnitude = 2;
var desk_hit : AudioClip;
var wall_hit : AudioClip;
var chair_hit : AudioClip;
var plant_hit : AudioClip;
var proj_hit : AudioClip;
function OnCollisionEnter(collision : Collision) {
if(gameObject == gameObject.FindWithTag ("Desk")){
AudioSource.PlayClipAtPoint(desk_hit, transform.position);
if(collision.relativeVelocity.magnitude > trigger_magnitude){
audio.volume = 1;
}else{
audio.volume = .5;
}
}if(gameObject == gameObject.FindWithTag ("Wall")){
AudioSource.PlayClipAtPoint(wall_hit, transform.position);
if(collision.relativeVelocity.magnitude > trigger_magnitude){
audio.volume = 1;
}else{
audio.volume = .5;
}
}if(gameObject == gameObject.FindWithTag ("Plant")){
AudioSource.PlayClipAtPoint(plant_hit, transform.position);
if(collision.relativeVelocity.magnitude > trigger_magnitude){
audio.volume = 1;
}else{
audio.volume = .5;
}
}if(gameObject == gameObject.FindWithTag ("Chair")){
AudioSource.PlayClipAtPoint(chair_hit, transform.position);
if(collision.relativeVelocity.magnitude > trigger_magnitude){
audio.volume = 1;
}else{
audio.volume = .5;
}
}if(gameObject == gameObject.FindWithTag ("Projectile")){
AudioSource.PlayClipAtPoint(proj_hit, transform.position);
if(collision.relativeVelocity.magnitude > trigger_magnitude){
audio.volume = 1;
}else{
audio.volume = .5;
}
}
}
Any help would be much appreciated!
Thanks in advance
-Ace