I’m trying to make a script that will destroy the object i click on. as long as it is with in a sertan range of the player.
I’ve gotten the range part down but when i click on one object it destroys all instances with in the range.
I need help isolating the script to destroying only the object i click on.
var target : Transform;
var com : Transform;
var range =10;
private var derp = 0;
var hit : RaycastHit;
function Update(){
if (Vector3.Distance(target.position,com.position) > range){
return;}
var mainCamera = FindCamera();
print(FindCamera());
// We need to actually hit an object
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100)){
print("raycast"+!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100));
return;}
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic){
if (Input.GetMouseButtonDown (1)){
Destroy(gameObject);
return;}
return;}
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}