- Home /
Question by
BawsAnimations · Mar 28, 2013 at 05:12 PM ·
collisionrigidbodyraycastforce
Adding Rigidbody to an Object on Collision by Raycast?
I have a plank shelter type of building and I want it's planks to fall off(add a rigidbody) when I shoot it with the raycast(bullets). This is an FPS game by the way.
My current script can add force to a rigidbody. BUT, I need HELP making the planks on the plank building to add a rigidbody to it when it's hit by a raycast.
This is my current code, Please please refine/fix it and post back please =)
function RayShoot (){
var hit : RaycastHit;
var direction : Vector3 = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , direction * Range , Color.blue);
if(Physics.Raycast(transform.position , direction , hit, Range)){
if(hit.rigidbody){
hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
}
if (hit.gameObject){
GameObject Plank = GameObject.FindGameObjectsWithTag("plank");
Plank.AddComponent.rigidbody;
}
}
BulletsLeft --;
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if( BulletsLeft == 0 ){
if(Input.GetKey(KeyCode.R)){
Reload();
}
}
}
Comment
Answer by whydoidoit · Mar 28, 2013 at 05:21 PM
That would look like this:
if(Physics.Raycast(transform.position , direction , hit, Range)){
if(!hit.rigidbody){
hit.gameObject.AddComponent(Rigidbody);
}
hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
}