x


How to Instantiate a line or a ray that goes through all colliders and send a message to them????

well, I'd like to Instantiate a line from my camera and make it go through all colliders in the scene, then I apply the function SendMessage() so that I can apply a damage to it; Here's the code:

var proiettile : GameObject;
var damange : int = 10;
var tempo_tra_colpi : float = 0.2; 

function Start(){


}

function FixedUpdate () {
if (Input.GetButton("Fire1")){
if (Time.time >= tempo_tra_colpi){
    Shoot();


 }
 }
}
function Shoot(){
var ray : Ray =     camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2,0));
Debug.DrawRay(ray.origin, ray.direction*Mathf.Infinity, Color.blue);
var hit: RaycastHit;

bullet = Instantiate(proiettile, ray.origin, Quaternion.identity);
Physics.IgnoreCollision(collider, bullet.collider);
bullet.rigidbody.AddForce(ray.direction*1000, ForceMode.Force);
Destroy(bullet, 3);





if(Physics.Linecast(ray.origin, ray.origin.forward*1000, hit)){
    for (var i : GameObject in hit){

    i.transform.SendMessageUpwards("ApplyDamage", damange, SendMessageOptions.DontRequireReceiver);
} 
}



  }
more ▼

asked Jun 08 '11 at 07:19 PM

anwe gravatar image

anwe
114 31 34 38

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Use this:

http://unity3d.com/support/documentation/ScriptReference/Physics.RaycastAll.html

It will run through and give you a list of everything hit by the raycast. Then use that list to call functions on them.

more ▼

answered Jun 08 '11 at 07:27 PM

almo gravatar image

almo
1.7k 2 6 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1525
x161
x42

asked: Jun 08 '11 at 07:19 PM

Seen: 528 times

Last Updated: Jun 08 '11 at 07:27 PM