- Home /
Question by
anwe · Jun 08, 2011 at 07:19 PM ·
raycastraycasthitlinecast
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);
}
}
}
Comment
Answer by almo · Jun 08, 2011 at 07:27 PM
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.
Your answer
Follow this Question
Related Questions
Physics.Raycast not hitting anything 1 Answer
Problem with raycast vertical 1 Answer
Help with Raycast C# 0 Answers
RaycastHit always returns 0 1 Answer
C# raycast shooting script broken 1 Answer