I have this current script (see below), what i’m trying to do is have a wall (made up of seperate blocks) and when you touch a block it adds force and falls down with gravity.
In my scene I just have “castle_brick” prefabs which at the moment are just cubes with rigidbody attached, duplicated to form a wall.
What is happening at the moment is when I touch any brick it plays the sound multiple times and all the bricks fly away together (as though one touch on any brick is hitting them all at once)
any help in what i’m doing wrong would be appriciated, cheers
private var ray : Ray;
private var rayCastHit : RaycastHit;
var mySound: AudioClip;
var bloodPrefab : GameObject;
var direction = transform.forward;
var force = 2000;
function Update(){
if(Input.GetMouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit)){
//add name of object/button to affect in ""
if(rayCastHit.transform.name == "castle_brick"){
//play sound
audio.clip = mySound;
audio.PlayOneShot(mySound);
Debug.Log("Score!");
Scorecounter.Counter +=5;
//create blood prefab
var rot = Quaternion.FromToRotation(Vector3.up, rayCastHit.normal);
Instantiate(bloodPrefab, rayCastHit.point, rot);
//add force to object
rigidbody.AddForceAtPosition(force * direction, rayCastHit.point);
Debug.Log ("hit castle_brick!");
}
}
}
}