I’m trying to destroy an object close to the main character of the game. I modified a script I found on the forum to calculate and compare distance between 2 objects when my mouse is over that object. The script is assigned to the objects I want to destroy.
My problem is that is I add a Input.GetButtonDown condition is stops working. I’m sure this will sound pretty silly, but i’m new to Java and I don’t code much… Any help is appreciated.
var target : Transform;
var distanciaMinima = 10;
public var distance : float;
function Start(){
target = GameObject.Find("Character").transform;
}
function OnMouseOver(){
distance = Vector3.Distance (transform.position, target.position);
//Debug.Log(distance);
if (distance < distanciaMinima Input.GetButtonDown("Mange"))
{
Debug.Log("Near");
}
}
i prefer not using Input.GetButtonDown, i prefer Input.GetKeyDown(KeyCode.Key), but i guess it makes no difference, try this code:
var target : Transform;
var distanciaMinima = 10;
public var distance : float;
function Start(){
target = GameObject.Find("Character").transform;
}
function OnMouseOver(){
distance = Vector3.Distance (transform.position, target.position);
//Debug.Log(distance);
if (distance < distanciaMinima Input.GetKeyDown(KeyCode.Mouse0))
{
Debug.Log("Near");
}
}
this would make it so if you left click on it, it would destroy, change the part KeyCode.Mouse0 to any button you want, use Unity - Scripting API: as a reference. I would, personally, use this script instead though, I dont think it makes a difference at all scripting wise though…
var distanciaMinima = 10;
public var distance : float;
function OnMouseOver(Other : Collider){
if(Other.transform.name = "Character"){
distance = Vector3.Distance (transform.position, Other.transform);
//Debug.Log(distance);
if (distance < distanciaMinima Input.GetKeyDown(KeyCode.Mouse0))
{
Debug.Log("Near");
}
}
}
In case this is in use for you, I wanted to do a “expansive shockwave” kind destruction, this is, the shockwave destroy while it grows around.
The above solution are not practical, and a super simple (total delegate) approach is to use a invisible rigid body (sphere for example) that scale itself at the same speed that the wave, everything it collisions in its way, you send a destroy message.
It is simple and “intuitive”
(of course, if you use rigid bodies for your objects ;-).
Greetings.