Search Unity

Raycast sendmessage, receive message from empty object

Discussion in 'Scripting' started by Deleted User, Dec 20, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hey! So i'm facing a small problem in Unity, and i'm not too sure how to describe it, so i'll try my best. So what I have is a gun, and then that gun shoots, if it hits an object with a tag, it'll play a bullet impact sound for that tag. Instead of having all of the audio stuff in the same script as the weapon shooting script, I want to have them in a separate script that is for bullet impact audio. So in my scene I have an empty object called WorldAudio, which has empty objects inside it like Bullet_Impact_Wood, which contain audio sources. For the raycast shooting for the gun, this is the script I have:
    Code (JavaScript):
    1. function createBulletHoles() {
    2.     var hit : RaycastHit;
    3.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    4.  
    5.     var direction : Vector3 = transform.forward;
    6.     if (isAds) {
    7.         direction.x += 0.05;
    8.         direction.y += 0.05;
    9.         direction.z += 0.05;
    10.     } else {
    11.         direction.x += Random.Range(-spreadFactor, spreadFactor);
    12.         direction.y += Random.Range(-spreadFactor, spreadFactor);
    13.         direction.z += Random.Range(-spreadFactor, spreadFactor);
    14.     }
    15.  
    16.     if (Physics.Raycast (transform.position, direction, hit, 10000)) {
    17.         hit.transform.SendMessage("WorldAudio", hit.transform.tag, SendMessageOptions.DontRequireReceiver);
    18.         var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    19.         var instantiated = Instantiate(bulletHole, hit.point, hitRotation);
    20.         Destroy(instantiated, 15);
    21.     }
    22. }
    the script also instantiates bullet holes where the raycast hits, and has a small spread to the raycast. So I have a raycast send message line, which sends the tag of the object to a function called WorldAudio. If that function is linked to the object that it hits, it works, but I want to have the shooting script send a message to the worldaudio object which has a script attached to it to tell it that an object was hit, and that it should play the audio for that object, but it doesn't work. Anyone understand what i'm trying to say? Know the solution to it?
     
  2. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    Why are you destroying this ??

    Code (CSharp):
    1. Destroy(instantiated, 15);
     
  3. Deleted User

    Deleted User

    Guest

    Code (JavaScript):
    1. var worldAudio : GameObject
    2.  
    3. if (Physics.Raycast (transform.position, direction, hit, 10000)) {
    4.         var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    5.         var instantiated = Instantiate(bulletHole, hit.point, hitRotation);
    6.         Destroy(instantiated, 15);
    7.         worldAudio.GetComponent(WorldAudio).WorldAudio(hit.transform.tag);
    8.     }
    Ok, so I solved my own problem, instead of using the raycast SendMessage, I decided to just call the worldaudio script from the shooting script and call the function, this is how I did it:
     
  4. Deleted User

    Deleted User

    Guest

    That's the bullet hole, they show up when the gun shoots, and disappear 15 seconds later
     
  5. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    nm, that's java script, I hardly use that, I was looking for the invoke.. I do not use Send message its to expensive.

    Glad you figured it out..